Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Serializable方法将HashMap(字符串,布尔值)传递给nextActivity_Android_Android Intent_Serialization_Hashmap - Fatal编程技术网

Android 使用Serializable方法将HashMap(字符串,布尔值)传递给nextActivity

Android 使用Serializable方法将HashMap(字符串,布尔值)传递给nextActivity,android,android-intent,serialization,hashmap,Android,Android Intent,Serialization,Hashmap,我有一个Hashmap,我想将其传递到下一个活动,所有答案都显示此方法-: Intent intent=new Intent(MainActivity.this,NextActivity.class); Bundle extras = new Bundle(); extras.putSerializable("YourHashMap",hashMap); intent.putExtras(extras); startActivity(intent); 但它显示了错误的第二个参数,它需要可序列化

我有一个Hashmap,我想将其传递到下一个活动,所有答案都显示此方法-:

Intent intent=new Intent(MainActivity.this,NextActivity.class);
Bundle extras = new Bundle();
extras.putSerializable("YourHashMap",hashMap);
intent.putExtras(extras);
startActivity(intent);
但它显示了错误的第二个参数,它需要可序列化的非映射字符串,布尔>

我甚至做了基本的尝试-:

intent.putExtra("myMap",myMap);

但它说它无法解析方法

尝试此方法传输您的值,对象必须可序列化,根据您的要求更改参数

HashMap<String, Boolean> hashMap = new HashMap<String, Boolean>();
hashMap.put("key", "value");
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("map", (Serializable)hashMap);
startActivity(intent);
检查是否正确

Boolean b= hashMap.get("key");

    if(b==true){
    //Your Toast Message
    }
试试这个,让我知道它是否适合你。

试试这个

 // pass HashMap from one Activity
                Intent intent = new Intent(this, AboutActivity.class);

                HashMap<String, Boolean> map = new HashMap<>();
                map.put("var",true);
                Bundle bun = new Bundle();
                bun.putSerializable("map", map);
                intent.putExtra("bundle",bun);

                startActivity(intent);
                overridePendingTransition(R.anim.slide_from_right, R.anim.nothing);


                // get HashMap from another activity
                Bundle  bundle = getIntent().getBundleExtra("bundle");
                if(bundle != null){
                    HashMap<String,Boolean> map = (HashMap<String, Boolean>) bundle.getSerializable("map");
                    if(map != null){
                        Log.e("bundle",map.get("var")+"");
                    }

                }

尝试使用singleton类使用setter方法存储hashmap,并使用getter方法在其他活动上获取它。

这是我尝试过的,并且有效-: //添加数据

Intent ActivityIntent = new Intent(MainActivity.this, NewActivity.class);
                Bundle bundles = new Bundle();
                bundles.putSerializable("myMap", (Serializable) myMap);
                ActivityIntent.putExtra("bundle",bundles);
//提取数据

 Bundle  bundle = getIntent().getBundleExtra("bundle");
        HashMap<String, Boolean> myMap = (HashMap<String, Boolean>) bundle.getSerializable("myMap");
        if(myMap.get(StringOfItemWhichIsTrue)) {
            Toast.makeText(this, "Works", Toast.LENGTH_SHORT).show();
        }

我建议您传递一个对象,而不是HashMap。您可以考虑在其中创建一个带有哈希映射的对象。使对象可包裹,然后将其传递给intent。正如我在上面所指定的,这种基本方式表明该方法未解析。请尝试,然后说哪一行表示您无法重新解析。我再次尝试,它表示无法解析方法putetrastring,Map;检查更新的答案我添加了序列化错误!。。。我没有在Hashmap之前添加Serializable..它不再给出错误..一旦成功,将接受您的答案..谢谢!我会再打给你的
 Bundle  bundle = getIntent().getBundleExtra("bundle");
        HashMap<String, Boolean> myMap = (HashMap<String, Boolean>) bundle.getSerializable("myMap");
        if(myMap.get(StringOfItemWhichIsTrue)) {
            Toast.makeText(this, "Works", Toast.LENGTH_SHORT).show();
        }