我将hashmap中的数据发送回来自子活动的父活动
这是我的代码 儿童活动
private void sendDataToPrevPg(HashMap<String, String> hm) {
// Send to previous activity page
Bundle bndle = new Bundle();
bndle.putSerializable("stockList", (Serializable) hm);
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("stkList",bndle);
setResult(RESULT_OK, intent);
this.finish();
}
在父母
中@SuppressWarnings("unchecked")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK ) {
if (data.hasExtra("stkList")) {
Bundle wrapper = getIntent().getBundleExtra("stkList");
HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stkList");
System.out.println("...serialized data4.."+myClass3);
}
}
}
在第
行显示错误 HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stkList");
logcat的:: 引起:java.lang.NullPointerException
如何在hashmap中发送数据?到父母的活动
答案 0 :(得分:3)
我认为你只是错过了关键&#34; stkList&#34;从捆绑包中检索HashMap时。它应该是&#34; stockList&#34;代替。
改变这个:
HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stkList");
到此:
HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
答案 1 :(得分:2)
private void sendDataToPrevPg(HashMap<String, String> hm) {
// Send to previous activity page
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("stkList",bndle);
setResult(RESULT_OK, intent);
this.finish();
}
在父活动中您可以接收HashMap值:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK ) {
if (data.hasExtra("stkList")) {
HashMap<String, String> myClass3 = (HashMap<String, String>) data.getSerializable("stkList");
System.out.println("...serialized data4.."+myClass3);
}
}
}
答案 2 :(得分:1)
我在这里展示示例代码供您参考。我刚试过这段代码,它对我来说很好。检查一下:
MainActivity:
final HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(1, "Hi");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("h", hashMap);
startActivity(intent);
}
});
SecondActivity:
Toast.makeText(SecondActivity.this,“Hi”+ getIntent()。getSerializableExtra(“h”)。toString(),Toast.LENGTH_SHORT).show();
答案 3 :(得分:0)
试试这个
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK ) {
if (data.hasExtra("stkList")) {
Bundle wrapper = getIntent().getBundleExtra("stkList");
HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
System.out.println("...serialized data4.."+myClass3);
}
}
}
答案 4 :(得分:0)
你把错误的钥匙。将错误行更改为
HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
答案 5 :(得分:0)
问题出在这行代码中
HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stkList");
它应该是stockList而不是stkList,如下所示
HashMap<String, String> myClass3 = (HashMap<String, String>) wrapper.getSerializable("stockList");
答案 6 :(得分:0)
使用数据代替getIntent()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK ) {
if (data.hasExtra("stkList")) {
Bundle wrapper = data.getBundleExtra("stkList");----data instead getIntent()
if(wrapper!=null){
HashMap<String, String> stockDtls = (HashMap<String, String>)wrapper.getSerializable("stockList");
}
}
}
}