这是我的Manual.java文件:
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;
}
}
}
然后是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);
}
}
然后是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>
我已经在AndroidManifest中添加了这两个类。
当我单击Manual.java上的创建按钮时,它总是崩溃。我班上有什么问题?
答案 0 :(得分:4)
您没有为捆绑设置数字,您应该致电Bundle#putInt
:
Bundle b = new Bundle();
Integer number = Integer.parseInt(NoQues.getText().toString());
b.putInt("key", number);
intent.putExtras(b);
第二个问题(导致崩溃)是,你应该设置文本,而不是int:
title.setText("" + value);
否则它会查找id = value的字符串,并且这样的id不存在(参见TextView#setText(int)
)。
答案 1 :(得分:0)
手动
Intent intent = new Intent(Manual.this, MCQSample.class);
intent.putExtras("val",NoQues.getText().toString());
startActivity(intent);
MCQSample
int value= Integer.parseInt( getIntent().getExtras().getString("val") );