从另一个屏幕打开另一个屏幕
问题描述:
我试图在Android应用程序中打开另一个屏幕。我从在线教程中复制了以下代码的大纲,然后尝试替换自己的屏幕。当我尝试运行以下代码时,当单击第一个屏幕上的按钮(第二个屏幕从不显示)时,我会看到“强制关闭”。有人能告诉我我做错了什么吗?有四个文件:ScreenTestActivity.java,main.xml,screen2.java和screen2.xml。从另一个屏幕打开另一个屏幕
ScreenTestActivity.java
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ScreenTestActivity extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(ScreenTestActivity.this, screen2.class);
startActivity(i);
}
});
}
}
的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Screen"
/>
</LinearLayout>
screen2.java:
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.os.Bundle;
public class screen2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
}
}
screen2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the New Screen"
/>
</LinearLayout>
答
<activity android:name="screen2"></activity>
<activity android:name="ScreenTestActivity"></activity>
把这个2升ine代码在你的androidmanifest.xml文件中
我在你的代码中没有看到任何问题,你有没有在清单文件中注册你的第二个活动? – vitakot 2011-12-17 18:46:30
谢谢。这解决了它。 – 2011-12-17 19:36:41