在 onActivityResult 中,我从收到的意图中得到空值
In onActivityResult i am getting null value from received intent
我有两个活动:A 和 B。
In activity A:在 "btn_navSimilarColor" buttonClick 上 - 我用 startActivityForResult 调用了 B。 A 中已经有一些使用相机和图库的意图以及我从之前的 activity 收到的意图数据。
在 activity B 中:我在 onCreate() 中调用了 asyncTask,在 asyncTask 的 onPostExecute() 中,我将额外的意图发送回 Activity A。
Activity答:
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"));
}
}
}
}
Activity乙:
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();
}
});
}
}
}
问题:
现在的问题是我正在 activity B 中获取数据 - 因为我已经用
检查了它
Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();
但是在 Activity A 的Activity结果中,我没有得到 "intnt_similarColor" 的捆绑包额外数据,即:
String stt = extras.getString("intnt_similarColor");
相反,我在 onCreate().
中获得 "xtra_selectedColor" 的额外捆绑包
为什么会这样,我如何获取以前的捆绑数据,而不是从 activity B 传递过来的数据?
将Bundle extras = getIntent().getExtras();
更改为Bundle extras = data.getExtras();
从您从 onActivityResult 收到的数据意图中获取字符串。您使用 Bundle extras = getIntent().getExtras();
,其中 getIntent() 实际上是 class A 收到的意图。
所以你必须使用:
String stt = data.getStringExtra("intnt_similarColor");
我有两个活动:A 和 B。
In activity A:在 "btn_navSimilarColor" buttonClick 上 - 我用 startActivityForResult 调用了 B。 A 中已经有一些使用相机和图库的意图以及我从之前的 activity 收到的意图数据。
在 activity B 中:我在 onCreate() 中调用了 asyncTask,在 asyncTask 的 onPostExecute() 中,我将额外的意图发送回 Activity A。
Activity答:
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"));
}
}
}
}
Activity乙:
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();
}
});
}
}
}
问题:
现在的问题是我正在 activity B 中获取数据 - 因为我已经用
检查了它Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();
但是在 Activity A 的Activity结果中,我没有得到 "intnt_similarColor" 的捆绑包额外数据,即:
String stt = extras.getString("intnt_similarColor");
相反,我在 onCreate().
中获得 "xtra_selectedColor" 的额外捆绑包为什么会这样,我如何获取以前的捆绑数据,而不是从 activity B 传递过来的数据?
将Bundle extras = getIntent().getExtras();
更改为Bundle extras = data.getExtras();
从您从 onActivityResult 收到的数据意图中获取字符串。您使用 Bundle extras = getIntent().getExtras();
,其中 getIntent() 实际上是 class A 收到的意图。
所以你必须使用:
String stt = data.getStringExtra("intnt_similarColor");