Android 未能将结果 ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} 传递给活动;java.lang.NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11083021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:56:56  来源:igfitidea点击:

Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity; java.lang.NullPointerException

androidnull

提问by Mani

Im creating Triggers and Action Application for my Final Year Project,

我正在为我的最后一年项目创建触发器和操作应用程序,

I return a child Activity result to intermediate activity and ther add some data to that activity and send it again to main activity,

我将一个子活动结果返回到中间活动,然后向该活动添加一些数据并将其再次发送到主活动,

I did for both Trigger sub module and Action sub module, both like same coding....

我为 Trigger 子模块和 Action 子模块做了同样的编码......

trigger module is working perfectly, but when action module run application is force stopped

触发模块工作正常,但是当动作模块运行应用程序被强制停止时

and error is

错误是

E/AndroidRuntime(5104): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {mani.droid.mechanize/mani.droid.mechanize.ActionListActivity}: java.lang.NullPointerException

Child onActivityResult

子活动结果

public void onSave(View v)
{
    if(txtNum.length() != 0)
    {
        String strTmp = null;
        Intent resultInt = new Intent();
        strTmp = txtNum.getText().toString();
        resultInt.putExtra("Num", strTmp);
        resultInt.putExtra("SubName", strTmp);
        setResult(Activity.RESULT_OK, resultInt);
        finish();
    }
    else
        Toast.makeText(getApplicationContext(), "Please enter number or choose from contact", Toast.LENGTH_SHORT).show();
}

Intermediate onActivityResult

中级 onActivityResult

//getting result from other activity and sending to action list activity
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK)
    {
        switch(reqCode)
        {
            case 1:
                    data.putExtra("ActionName", txtAction);
                    setResult(Activity.RESULT_OK, data);
                    finish();
                    break;
        }
    }
}

Main onActivityResult

主要 onActivityResult

//Getting Trigger parameters and adding to list
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK)
    {
        switch(reqCode)
        {
            case 1:
                if (data.equals(null))
                {
                    Toast.makeText(context, "Intent is Null", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    //String actName = data.getStringExtra("ActionName");
                    String subName = data.getStringExtra("SubName");
                    Toast.makeText(context, subName, Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
} 

回答by Sean Owen

It means the Intentreceiver threw an exception in its onActivityResult(). And I can see the NullPointerExceptionright here: data.equals(null)is definitely wrong as it throws an exception when datais null. You mean data == null.

这意味着Intent接收器在其onActivityResult(). 我可以在NullPointerException这里看到正确的:data.equals(null)绝对是错误的,因为它在datais时抛出异常null。你的意思是data == null

回答by Mina Fawzy

just check null != data, as data equal null

只需检查null != data,因为数据等于空