开发者

How to Open New Screen in Android

I am following this article to learn how to open new screen on button click:

http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html

But I am getting 3 error messages like this:

      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

This is what I have done till now:

screen1.java

    package test.android;


import android.app.Activity;
import android.content.Intent;
import android.os.Bu开发者_开发百科ndle;
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>

To give you an idea, this is screenshot of my project

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

Please help guys !


Instead of setting your button ids as id ="@+id/btnClick" , use android:id ="@+id/btnClick" It is always a good idea to read the comments of the tutorials that you follow if you have problems, this fix was just 3-4 comments down.

Also if you look at the line numbers referenced: 18 in screen1.java and 13 in screen2.java, you'll find that the problem happens when you reference your button ids.


Ok, my first answer was nonsense. But I've noticed that you're declaring a screen1 activity inside a screen1 activity in your manifest. You only need 1, with the label "@string/app_name" and with the intent filter.

Try changing that, clean the project and let me know if anything changes.


What is your import android.R doing? I think your R.java is not generated? is it?

Coz i just worked on it on my eclipse i didnt find import android.R? and it worked completely fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜