简体   繁体   English

使用onActivityResult获取空意图

[英]Getting a null intent with onActivityResult

Here is the code from the MainActivity that calls the intent: 这是MainActivity中调用该意图的代码:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, NewNote.class);
            startActivityForResult(intent, REQUEST_CODE);

        }
    });

It then goes to the NewNote class which does this with the intent: 然后转到NewNote类,其目的是这样做的:

Intent intent = new Intent();
                intent.putExtra("title", title.getText().toString());

                //set the result, it will be passed to onActivityResult() in MainActivity
                setResult(RESULT_OK, intent);
                finish();

Finally, inside the MainActivity I have the onActivityResult method: 最后,在MainActivity内部,我具有onActivityResult方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);

    if (resultCode == RESULT_OK) {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String value = extras.getString("title");
            //The key argument here must match that used in the other activity
            Toast.makeText(this, "" + value, Toast.LENGTH_SHORT).show();
        }
    }
}

When I run the app, the OnActivityResult method works fine however the output by the value variable is always null. 当我运行应用程序时,OnActivityResult方法可以正常工作,但是value变量的输出始终为null。

Change: 更改:

Bundle extras = getIntent().getExtras();

To: 至:

Bundle extras = data.getExtras();

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM