我正在关注本文,以了解如何在单击按钮时打开新屏幕:
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

请帮助大家!

最佳答案

不要将按钮ID设置为id ="@+id/btnClick",而应使用android:id ="@+id/btnClick"
如果遇到问题,请阅读本教程的注释,这总是一个好主意,此修复程序仅减少了3-4条注释。

同样,如果您查看引用的行号:screen1.java中的18和screen2.java中的13,则当您引用按钮ID时,就会发现问题发生了。

关于android - 如何在Android中打开新屏幕,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6681758/

10-12 00:19
查看更多