Heim > Fragen und Antworten > Hauptteil
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent loginIntent = new Intent(login.this,MainActivity.class);
number = SNumber.getText().toString();
password = PassWord.getText().toString();
xmlThread xmlThread = new xmlThread(url,number,password,loginIntent);
xmlThread.start();setResult(Activity.RESULT_OK,loginIntent);
finish();
}
});
xmlThread中的将数据通过intent传递
userDataList = updatas;
intent.putExtra("imageUrl",userDataList.get(0).getImageUrl());
intent.putExtra("SName", userDataList.get(0).getSName());
intent.putExtra("SNumber", userDataList.get(0).getSNumber());
intent.putExtra("SClass", userDataList.get(0).getSClass());
Log.e("main",userDataList.get(0).getImageUrl());
主函数中点击事件,进入登录界面。
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,login.class), REQUSET);
}
});
获取Intent中的参数
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data!=null) {
switch (requestCode) {
case REQUSET:
imageUrl = data.getBundleExtra("imageUrl").toString();//从这行就开始报错了
new HttpImageButton(imageUrl, handler, imageButton);
TName = data.getBundleExtra("SName").toString();
TNumber = data.getBundleExtra("SNumber").toString();
TClass = data.getBundleExtra("SClass").toString();
Toast.makeText(this, imageUrl + TName + TNumber + TClass, Toast.LENGTH_LONG).show();
handler.post(new Runnable() {
@Override
public void run() {
name.setText("1");
SNumber.setText("2");
SClass.setText("3");
}
});
break;
default:
break;
}
}
}
错误提示为java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { cmp=com.tjapp.login1_2/.MainActivity }} to activity {com.tjapp.login1_2/com.tjapp.login1_2.MainActivity}: java.lang.NullPointerException,报出来一个空指针。![图片描述][1]
迷茫2017-04-17 16:00:15
看起来像是线程同步问题
xmlThread.start();
setResult(Activity.RESULT_OK,loginIntent);
xmlThread可能都还没执行起来,就已经setResult了,intent中的imageUrl还没初始化并赋值,所以报空指异常。
阿神2017-04-17 16:00:15
代码贴的太凌乱了。提供一种思路,注意MainActivity的启动模式,假如是单实例的话,startActivityForResult会立即返回到onActivityResult