activity互相传值报错。。
错误提示
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=0, data=null} to activity {包名/类名}: java.lang.NullPointerException
启动新ACTICITY的代码
Intent intent=new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//增加flag
intent.setClass(PhotoAttaActivity.this, PhotographActivity.class);
//設定傳送參數
Bundle bundle = new Bundle();
bundle.putString("name1", "value1");
intent.putExtras(bundle);//將參數放入intent
PhotoAttaActivity.this.startActivityForResult(intent,10);
接收返回部分的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String result=data.getExtras().getString("result");
Toast.makeText(PhotoAttaActivity.this, result+"fewfewfwef", Toast.LENGTH_SHORT);
}
新ACTIVITY在关闭事件中的代码
Intent intent2=this.getIntent();
intent2.putExtra("result", (result.length()==0)?result:result.substring(1));
setResult(Activity.RESULT_OK, intent2);//
this.finish();
[解决办法]
String result=data.getExtras().getString("result");
改为:
String result=data.getExtras().getString("name1");