android的Activity跳转,一直提示我有空指针..
publicclassActivity03extendsActivity{/**Calledwhentheactivityisfirstcreated.*/private...
public class Activity03 extends Activity {
/** Called when the activity is first created. */
private EditText number1 = null;
private EditText number2 = null;
private TextView chuhao = null;
private Button getResult = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据控件的ID来取得代表控件的对象
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
chuhao = (TextView)findViewById(R.id.chuhao);
getResult = (Button)findViewById(R.id.getResult);
//将监听器绑定在按钮对象上面
getResult.setOnClickListener(new getResultOnClickListener());
chuhao.setText(R.string.chuhao);
getResult.setText(R.string.getResult);
}
//监听器
class getResultOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//取得两个计算的数值
String number1Str = number1.getText().toString();
String number2Str = number2.getText().toString();
//将这两个值存放到Intent对象中
Intent intent = new Intent();
int number1Int = Integer.parseInt(number1Str);
int number2Int = Integer.parseInt(number2Str);
int result = number1Int/number2Int;
intent.putExtra("result",result);
//使用Intent对象启动ResultActivity
intent.setClass(Activity03.this, ResultActivity.class);
Activity03.this.startActivity(intent);
}
}
}
----------------------
另一个Activity
public class ResultActivity extends Activity{
private TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.result);
super.onCreate(savedInstanceState);
//得到Intent对象当中的值
Intent intent = getIntent();
String result = intent.getStringExtra("result");
resultView = (TextView)findViewById(R.id.resultOf);
resultView.setText(result);
} 展开
/** Called when the activity is first created. */
private EditText number1 = null;
private EditText number2 = null;
private TextView chuhao = null;
private Button getResult = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据控件的ID来取得代表控件的对象
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
chuhao = (TextView)findViewById(R.id.chuhao);
getResult = (Button)findViewById(R.id.getResult);
//将监听器绑定在按钮对象上面
getResult.setOnClickListener(new getResultOnClickListener());
chuhao.setText(R.string.chuhao);
getResult.setText(R.string.getResult);
}
//监听器
class getResultOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//取得两个计算的数值
String number1Str = number1.getText().toString();
String number2Str = number2.getText().toString();
//将这两个值存放到Intent对象中
Intent intent = new Intent();
int number1Int = Integer.parseInt(number1Str);
int number2Int = Integer.parseInt(number2Str);
int result = number1Int/number2Int;
intent.putExtra("result",result);
//使用Intent对象启动ResultActivity
intent.setClass(Activity03.this, ResultActivity.class);
Activity03.this.startActivity(intent);
}
}
}
----------------------
另一个Activity
public class ResultActivity extends Activity{
private TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.result);
super.onCreate(savedInstanceState);
//得到Intent对象当中的值
Intent intent = getIntent();
String result = intent.getStringExtra("result");
resultView = (TextView)findViewById(R.id.resultOf);
resultView.setText(result);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询