在Android上从一项活动转移到另一项活动
问题描述:
我正尝试从一项活动转移到另一项活动上的简单Android应用程序代码。但它似乎并没有工作。一旦我点击按钮“更改活动”,该应用程序停止工作。这是两个活动的代码。在Android上从一项活动转移到另一项活动
1.主要活动。所述一个用户看到第一时,应用程序是向上
package eg.edu.guc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
Button b1;
TextView t1;
EditText e1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(HelloAndroidActivity.this,HelloAndroidActivity2.class);
HelloAndroidActivity.this.startActivity(intent);
//finish();
}
});
t1 = (TextView) findViewById(R.id.TextView1);
e1= (EditText) findViewById(R.id.editText1);
}
}
,按钮“改变活动”将用户带到
2.第二活动:
package eg.edu.guc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class HelloAndroidActivity2 extends Activity{
TextView t2;
ImageView I;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
t2 = (TextView) findViewById(R.id.TextView2);
I = (ImageView) findViewById(R.id.imageView1);
I.setImageResource(R.drawable.kareem);
}
}
3.Here是主要的XML UI对于第一个活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView1"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:text="Hello Android"
android:textSize="30dp" />
<EditText
android:id="@+id/editText1"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<Button
android:id="@+id/button1"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Activity" />
</LinearLayout>
4.And终于在这里是第二个活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Kareem's Photo" >
<requestFocus />
</TextView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.21"
android:src="@drawable/ic_launcher" />
</LinearLayout>
的UI XML文件
答
尝试Shashank的建议。如果它不工作,试试这个:
变化
HelloAndroidActivity.this.startActivity(intent);
为:
startActivity(intent);
答
你导入新的活动到AndroidManifest.xml中? 你应该插入水木清华像 <activity android:name=".HelloAndroidActivity2"></activity>
<application></application>
之间
我认为你缺少清单文件为您的活动HelloAndroidActivity2条目..... – 2012-03-02 09:46:38