确定父活动使用startActivityForResult调用BuyActivity并将3个值返回到父活动。其中一个是类别。有三类食品,个人和教育可供选择。当我选择教育或个人时我没有遇到任何问题,但是当我选择食物时我得到了这个错误,但是父活动仍然收到了所有返回的3个值。所以任何人都可以帮忙
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {apps.project/apps.project.ConsultExpert}: java.lang.NullPointerException
此函数调用startActivityForResult
private void displayBuyQuestionnaire() {
Intent intent = new Intent(this, BuyActivity.class);
startActivityForResult(intent, BUY_ADVICE);
}
在BuyActivity.class里面
Intent returnIntent = new Intent();
returnIntent.putExtra("item_name", iName);
returnIntent.putExtra("item_price", iPrice);
returnIntent.putExtra("item_category", iCategory);
setResult(RESULT_OK,returnIntent);
finish();
onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case BUDGET_ADVICE:
if (resultCode == RESULT_OK && data.hasExtra("status")) {
item_name = data.getStringExtra("status");
Log.d("return", data.getStringExtra("status"));
runBudgetTree();
}
else {
if(resultCode == RESULT_OK)
Log.d("msg", "result code ok tp x semua return");
}
break;
case BUY_ADVICE:
if (resultCode == RESULT_OK) {
item_name = data.getStringExtra("item_name");
item_category = data.getStringExtra("item_category");
item_price = data.getDoubleExtra("item_price", 0);
runBuyTree();
}
break;
}
logcat的
01-09 17:56:51.776:D / var i(3159):answer1 01-09 17:56:51.776:D / var 我(3159):answer2 01-09 17:56:51.776:D / var i(3159):answer3 01-09 17:56:51.781:D / AndroidRuntime(3159):关闭VM 01-09 17:56:51.781:W / dalvikvm(3159):threadid = 1:线程退出 未捕获的异常(组= 0x4001e578)01-09 17:56:51.786: E / AndroidRuntime(3159):致命异常:主01-09 17:56:51.786: E / AndroidRuntime(3159):java.lang.RuntimeException:传递失败 结果ResultInfo {who = null,request = 1,result = -1,data = Intent {(has extras)}} to activity {apps.project / apps.project.ConsultExpert}: java.lang.NullPointerException 01-09 17:56:51.786: E / AndroidRuntime(3159):at android.app.ActivityThread.deliverResults(ActivityThread.java:2536) 01-09 17:56:51.786:E / AndroidRuntime(3159):at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578) 01-09 17:56:51.786:E / AndroidRuntime(3159):at android.app.ActivityThread.access $ 2000(ActivityThread.java:117)01-09 17:56:51.786:E / AndroidRuntime(3159):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:965) 01-09 17:56:51.786:E / AndroidRuntime(3159):at android.os.Handler.dispatchMessage(Handler.java:99)01-09 17:56:51.786:E / AndroidRuntime(3159):at android.os.Looper.loop(Looper.java:130)01-09 17:56:51.786: E / AndroidRuntime(3159):at android.app.ActivityThread.main(ActivityThread.java:3691)01-09 17:56:51.786:E / AndroidRuntime(3159):at java.lang.reflect.Method.invokeNative(Native Method)01-09 17:56:51.786:E / AndroidRuntime(3159):at java.lang.reflect.Method.invoke(Method.java:507)01-09 17:56:51.786: E / AndroidRuntime(3159):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:907) 01-09 17:56:51.786:E / AndroidRuntime(3159):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)01-09 17:56:51.786:E / AndroidRuntime(3159):at dalvik.system.NativeStart.main(Native Method)01-09 17:56:51.786: E / AndroidRuntime(3159):引起:java.lang.NullPointerException 01-09 17:56:51.786:E / AndroidRuntime(3159):at apps.project.ConsultExpert.runBuyTree(ConsultExpert.java:364)01-09 17:56:51.786:E / AndroidRuntime(3159):at apps.project.ConsultExpert.onActivityResult(ConsultExpert.java:1769) 01-09 17:56:51.786:E / AndroidRuntime(3159):at android.app.Activity.dispatchActivityResult(Activity.java:3934)01-09 17:56:51.786:E / AndroidRuntime(3159):at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 01-09 17:56:51.786:E / AndroidRuntime(3159):... 11更多
答案 0 :(得分:1)
您必须放置displayBuyQuestionnaire()方法行
Intent intent = new Intent(this, BuyActivity.class);
由此
Intent intent = new Intent(<your class name>.this, BuyActivity.class);
答案 1 :(得分:0)
Thred已经过时了但仅供将来参考:
同样的问题花了我1小时,所以只是分享解决方案,对我有用。我试图从第一个Textview
中的第二个活动发送消息。 ResponseToFirst
是邮件ID,Message_fromsecond
是textview
。
早些时候onActivityResult
我正在使用
Bundle extras = getIntent().getExtras();
Message_fromsecond.setText(extras.getString("ResponseToFirst")); //crashed here
后来我发现我需要使用
String returnString = data.getExtras().getString("ResponseToFirst");
Message_fromsecond.setText(returnString);
了解更多信息查看Here。