this is my Manual.java file:
public class Manual extends Activity implements OnClickListener{
Button create;
TextView gotAnswer, NoAnsV;
EditText NoQues, NoAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.manualask);
NoQues = (EditText) findViewById (R.id.NoQues);
NoAnswer = (EditText) findViewById (R.id.NoAnsw);
create = (Button) findViewById (R.id.create);
create.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.create:
Intent intent = new Intent(Manual.this, MCQSample.class);
Bundle b = new Bundle();
Integer Number = Integer.parseInt(NoQues.getText().toString());
intent.putExtras(b);
startActivity(intent);
finish();
break;
}
}
}
Then the MCQSample.java:
public class MCQSample extends Activity{
TextView title;
String gotBread;
int value;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mcqsample);
title = (TextView) findViewById(R.id.abc);
Bundle b = getIntent().getExtras();
int value = b.getInt("key", 0);
title.setText(value);
}
}
Then the mcqsample.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Text"
android:id="@+id/abc"/>
</LinearLayout>
Both classes I have already added in the AndroidManifest.
When I click the create button on the Manual.java it always crashes. What wrong with my classes?
You don’t set the number to the bundle, you should call
Bundle#putInt:The second problem (which causes the crash) is, that you should set text, not int:
Otherwise it looks for a string with the id = value, and such id doesn’t exist (see
TextView#setText(int)).