开发者

Error with Intents (Android)

So this is supposed to be really really simple, since I'm just in the beginning stages of this app. Remember, I'm a beginner so excuse the noob code :p

The app crashes (force close) whenever I click the button to go on to the next page. Code is given below:

    Button b = (Button) findViewById(R.id.button1);
    final EditText et1 = (EditText) findViewById(R.id.editText1);
    final EditText et2 = (EditText) findViewById(R.id.editText2);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(User.this, Game.class);
            i.putExtra("p1", et1.getText().toString());
            i.putExtra("p2", et2.getText().toString());

            Toast.makeText(User.this, "Activity 2 about to launch", Toast.LENGTH_SHORT).show();
            Toast.makeText(User.this, "Activity 2 launched", Toast.LENGTH_SHORT).show();
            startActivity(i);
        }
    });
}

}

Next page is below:

TextView tv = (TextView) findViewById(R.id.textView2);

Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);    
Button b4开发者_高级运维 = (Button) findViewById(R.id.button4);
Button b5 = (Button) findViewById(R.id.button5);
Button b6 = (Button) findViewById(R.id.button6);    
Button b7 = (Button) findViewById(R.id.button7);
Button b8 = (Button) findViewById(R.id.button8);
Button b9 = (Button) findViewById(R.id.button9);

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    Toast.makeText(Game.this, "Welcome "+getIntent().getExtras().getString("p1")+" and "+getIntent().getExtras().getString("p2"), Toast.LENGTH_SHORT).show();

}


You cannot use findViewById before onCreate (and this is what you are doing if you try to initialise with a value like that).

You should also not try to resolve your views before you have called setContextView since they are not part of your layout before this time. You can only 'find' them once they have been added.

Move the initialising code to inside the onCreate method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

TextView tv = (TextView) findViewById(R.id.textView2);

Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);    
Button b4 = (Button) findViewById(R.id.button4);
Button b5 = (Button) findViewById(R.id.button5);
Button b6 = (Button) findViewById(R.id.button6);    
Button b7 = (Button) findViewById(R.id.button7);
Button b8 = (Button) findViewById(R.id.button8);
Button b9 = (Button) findViewById(R.id.button9);

Toast.makeText(Game.this, "Welcome "+getIntent().getExtras().getString("p1")+" and "+getIntent().getExtras().getString("p2"), Toast.LENGTH_SHORT).show();

}


I see one obvious problem.

From Intent.putExtra:

Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

i.putExtra("p1", et1.getText().toString());

"p1" does not have a package prefix! So, this may work, may not work, crash, or demons may fly out of your nose. You should change "p1" to something like "com.myfirstandroid.p1", assuming the package is called "com.myfirstandroid".

I can't be sure if this is your exact problem. You should include the reason of the crash for more specific answers. Have look at the LogCat.


A really common way to crash your application (especially when you are new to Android) is by forgetting to declare an Activity in your manifest file. I'm betting that this has caused your error.

Your code looks good, and it should start the Game Activity. However, even if you have properly defined the Game class, it will not work unless you declare Game in your manifest file. Your manifest should already include some code about your original Activity that looks something like this:

<activity android:name=".YourApplication"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This declares a new activity and adds some filters to make sure it is the first thing opened when you launch the application. You should make sure you add a line like this right below it, so that the system knows about your Game class

<activity android:name=".Game" android:label="Game">  

For future reference and debugging more complex errors, you will want to know about the LogCat.

The logcat usually makes fixing errors incredibly simple. When you get an error, the logcat output in Eclipse typically looks like this

07-25 13:19:10.593: ERROR/AndroidRuntime(22861): FATAL EXCEPTION: main
ERROR/AndroidRuntime(22861): java.lang.RuntimeException: /some sort of error/
    .../A lot of description/
    ...
07-25 13:19:10.593: ERROR/AndroidRuntime(22861): Caused by: /Whatever the specific error was/
    07-25 13:19:10.593: ERROR/AndroidRuntime(22861):at com.chris.formStuff.FormStuffActivity.onCreate(FormStuffActivity.java:25)
    ...
    ...

When you get errors like this you want to look at the "Caused by..." line, and the first few after that. This will describe the error and give you the exact line it occurred at.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜