从另一个屏幕打开一个屏幕

时间:2011-12-17 18:28:06

标签: java android android-layout

我正试图在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>

1 个答案:

答案 0 :(得分:0)

       <activity android:name="screen2"></activity>

       <activity android:name="ScreenTestActivity"></activity>

将这两行代码放在androidmanifest.xml文件中