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();
}
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:
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
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
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.