【问题标题】:In onActivityResult i am getting null value from received intent在 onActivityResult 我从收到的意图中得到空值
【发布时间】:2017-05-24 17:52:46
【问题描述】:

我有两个活动:A 和 B。

在活动 A 中:在“btn_navSimilarColor”按钮单击上 - 我使用 startActivityForResult 调用了 B。 A 内部已经有一些使用相机和图库的意图,以及我从以前的活动中收到的意图数据。

在活动 B 中:我在 onCreate() 中进行了 asyncTask 调用,在 asyncTask 的 onPostExecute() 中,我将额外的意图发送回活动 A。

活动 A:

public class A extends Activity 
{
...
@Override
public void onCreate(Bundle savedInstanceState)
{
  ...
  Bundle extras = getIntent().getExtras();
    if (extras != null) {
        edtTxtColorCode.setText(extras.getString("xtra_selectedColor"));
    } else {
        Toast.makeText(this, "There was a problem in the response!", Toast.LENGTH_SHORT).show();
    }
}


public void buttonOnClick(View view)
{
    switch (view.getId())
    {
        case R.id.btnCamera:
            startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), FLAG_CAMERA);

            break;

        case R.id.btnGallery:
            startActivityForResult(
                    new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI),  FLAG_GALLERY);
            break;

        case R.id.btn_navSimilarColor:
            Intent intnt_similar = new Intent(A.this, B.class);
            intnt_similar.putExtra("xtraColor", edtTxtColorCode.getText().toString());
            startActivityForResult(intnt_similar, FLAG_navSimilarColorAct);

            break;

        default:
            break;
    }

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v("resultCode","="+resultCode);
    if (resultCode == Activity.RESULT_OK)
    {
        mCursor = null;
        if (requestCode == FLAG_GALLERY)
            onSelectFromGalleryResult(data);
        else if (requestCode == FLAG_CAMERA)
            onCaptureImageResult(data);
        else if(requestCode == FLAG_navSimilarColorAct)
        {   Bundle extras = getIntent().getExtras();
            String stt = extras.getString("intnt_similarColor");
            if (extras != null)

edtTxtColorCode.setText(extras.getString("intnt_similarColor"));
        }
    }
}
 }

活动 B:

public class B extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    ....
     receiveIntent();
     new AsyncConver().execute();

    }

    private void receiveIntent() {
    Bundle extras = getIntent().getExtras();
    if (extras != null)
        strIntentrecvdColor = extras.getString("xtraColor");
    else
        Toast.makeText(this, "There was a problem in the response!", Toast.LENGTH_SHORT).show();
    }

     class AsyncConvert extends AsyncTask<String, Integer, String>
    {
    ...
     @Override
    protected void onPostExecute(String s) 
    {
        super.onPostExecute(s);

         Custom_SimilarColorListAdapter gridAdapter = new Custom_SimilarColorListAdapter(SimilarColors.this, list_SimilarColors);
        grdVw.setAdapter(gridAdapter);

        grdVw.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            String str_colorCodeSimilar = ((TextView) v.findViewById(R.id.listrow_similar_code)).getText().toString();
            Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();

            Intent retrnIntnt = new Intent();
            retrnIntnt.putExtra("intnt_similarColor", str_colorCodeSimilar);
            setResult(RESULT_OK, retrnIntnt);
            finish();

        }
        });

        }
    }

}

问题:

现在的问题是我在活动 B 中获取数据 - 因为我已经在检查它了

Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();

但在 Activity A 的 onActivityResult 中,我没有获得“intnt_similarColor”的捆绑额外数据,即:

    String stt = extras.getString("intnt_similarColor");

相反,我为 onCreate() 内部的“xtra_selectedColor”获得了额外的捆绑包。

为什么会发生这种情况,我是如何获得以前的捆绑数据的,而不是从活动 B 传递的数据?

【问题讨论】:

    标签: android android-intent android-bundle


    【解决方案1】:

    Bundle extras = getIntent().getExtras(); 更改为Bundle extras = data.getExtras();

    【讨论】:

      【解决方案2】:

      从您从 onActivityResult 收到的数据意图中获取字符串。您使用 Bundle extras = getIntent().getExtras(); 其中 getIntent() 实际上是 A 类接收到的意图。

      所以你必须使用:

      String stt = data.getStringExtra("intnt_similarColor");
      

      【讨论】:

        猜你喜欢
        • 2017-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多