如何在Android中打开新屏幕

时间:2011-07-13 15:49:37

标签: android android-layout android-manifest

我正在关注这篇文章,了解如何在按钮点击时打开新屏幕:
http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html

但我收到3条错误消息:

      Description   Resource    Path    Location    Type
R cannot be resolved to a variable  screen1.java    /screen1/src/test/android   line 20 Java Problem
R cannot be resolved to a variable  screen1.java    /screen1/src/test/android   line 21 Java Problem
R cannot be resolved to a variable  screen2.java    /screen1/src/test/android   line 13 Java Problem
R cannot be resolved to a variable  screen2.java    /screen1/src/test/android   line 14 Java Problem
Unparsed aapt error(s)! Check the console for output.   screen1     line 1  Android ADT Problem

这就是我到目前为止所做的事情:

screen1.java

    package test.android;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;




public class screen1 extends Activity
{
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.screen1);
      Button b = (Button)findViewById(R.id.btnClick);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
         Intent i = new Intent(screen1.this, screen2.class);
         startActivity(i);
         }
      });
   }
}

screen2.java

    package test.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class screen2 extends Activity
{
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.screen2);
      Button b = (Button) findViewById(R.id.btnClick2);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
         setResult(RESULT_OK);
         finish();
         }
      });
   }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.android"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
     <activity android:name=".Screen1" android:label="Screen 1">
        <activity android:name=".screen1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        </activity>
        <activity android:name=".Screen2"  android:label="Screen 2">
        </activity>

    </application>
</manifest>

screen1.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.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"
/>
<Button
   android:id ="@+id/btnClick2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Close"
/>

</LinearLayout>

为了给你一个想法,这是我的项目的截图

http://img39.imageshack.us/img39/8883/projscreen.jpg

请帮帮我们!

3 个答案:

答案 0 :(得分:3)

不要将按钮ID设为id ="@+id/btnClick",而是使用android:id ="@+id/btnClick" 如果您遇到问题,阅读您所遵循的教程的评论总是一个好主意,这个修复只是3-4评论。

另外,如果您查看引用的行号:screen1.java中的18行和screen2.java中的13行,您会发现在引用按钮ID时会出现问题。

答案 1 :(得分:0)

好的,我的第一个答案是胡说八道。但我注意到你在清单中的screen1活动中声明了screen1活动。您只需要1,标签为“@ string / app_name”,并带有intent过滤器。

尝试更改它,清理项目并告知我是否有任何变化。

答案 2 :(得分:-1)

你的导入android.R做什么? 我认为你的R.java没有生成?是吗?

因为我刚刚在我的日食上工作,我没有找到导入android.R?它完全没用。