我正试图在一个简单的Android应用程序代码上从一个Activity转移到另一个Activity。但它似乎根本不起作用。点击“更改活动”按钮后,应用程序就会停止工作。以下是这两项活动的代码。
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.这是第一个活动的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.最后这里是第二个活动的UI XML文件
<?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>
答案 0 :(得分:1)
您是否将新活动导入AndroidManifest.xml?
你应该插入像smth一样
<activity android:name=".HelloAndroidActivity2"></activity>
在<application></application>
之间
答案 1 :(得分:-1)
尝试Shashank的建议。如果它不起作用试试这个:
更改
HelloAndroidActivity.this.startActivity(intent);
有:
startActivity(intent);