将哈希映射传递给intent

时间:2017-03-08 09:51:29

标签: android android-studio android-intent hash

我从两个文本字段获取用户输入并将它们存储到哈希映射中。我已经检查过是否正确地将值插入到哈希映射中。他们是。一旦我尝试使用Intent.putExtra传递它,我就会收到此错误。

Unable to start activity ComponentInfo{com.example.test.retrofit/com.example.test.retrofit.SearchActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:158)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                              Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
                                                                                 at com.example.abdulhakim.retrofit.SearchActivity.onCreate(SearchActivity.java:42)
                                                                                 at android.app.Activity.performCreate(Activity.java:6876)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:158) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7225) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

这是我在MainActivity中的代码:

Map<String, String> data = new HashMap<String,String>();
input1 = (EditText) findViewById(R.id.term);
    input2 = (EditText) findViewById(R.id.column);
    ImageView =(ImageButton)findViewById(R.id.search);
    ImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SearchActivity.class);
            k=input1.getText().toString();
            t= input2.getText().toString();
            data.put("key",k);
            data.put("term",t);
            Log.v("HashMapTest KEY", data.get("key"));
            Log.v("HashMapTest TERM", data.get("term"));

            intent.putExtra("hash",data);
            startActivity(intent);

        }
    });

这是我的第二项活动:

Intent intent = getIntent();
    HashMap<String, String> data = (HashMap<String, String>)intent.getSerializableExtra("hash");


    if (data.isEmpty()) {
        Toast.makeText(getApplicationContext(), "nope! I didnt get the hashmap", Toast.LENGTH_LONG).show();
        return;
    }

3 个答案:

答案 0 :(得分:0)

使用此

   intent.putExtra("hash",data.toString());

OR

   intent.putSerializableExtra("hash",data)

答案 1 :(得分:0)

这非常简单,All Collections对象实现了Serializable(sp?)接口,这意味着它们可以在Intent中作为Extras传递

使用 putExtra(String key,Serializable obj)插入HashMap,另一个Activity使用 getIntent()。getSerializableExtra(String key),你需要将返回值强制转换为HashMap。

答案 2 :(得分:0)

试试这个:在你的第一项活动中:

HashMap<String, String> data = new HashMap<String,String>();  //initialize properly

Intent intent = new Intent(MainActivity.this, SearchActivity.class);
intent.putExtra("hashMap", data);
startActivity(intent);

现在使用第二个Activity获取数据:

Intent intent = getIntent();    
HashMap<String, String> hashMap = (HashMap<String, String>) intent.getSerializableExtra("hashMap");
String key = hashMap.get("key");
Sting term = hashMap.get("term");

问题在于您试图将activity投射到String

的第二个Hashmap