Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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
Java 通过intent传递serializable导致ClassCastException错误_Java_Android_Android Intent - Fatal编程技术网

Java 通过intent传递serializable导致ClassCastException错误

Java 通过intent传递serializable导致ClassCastException错误,java,android,android-intent,Java,Android,Android Intent,我正在从一个活动向另一个活动传递一个具有长[]和字符串的键值对的HashMap,然后将HashMap数据放入树映射中,以便对其进行排序。为了传递数据,我对HashMap数据进行了for循环 以下是发送意图的活动的代码: public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav

我正在从一个活动向另一个活动传递一个具有长[]和字符串的键值对的HashMap,然后将HashMap数据放入树映射中,以便对其进行排序。为了传递数据,我对HashMap数据进行了for循环

以下是发送意图的活动的代码:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(this, AnotherActivity.class);
    HashMap<Long[], String> hashmap = new HashMap<>();
    for (int i = 0; i < 10; i++) {
        hashmap.put(new Long[]{(long) i * 20, (long) i * 30}, "home" + i);
    }
    intent.putExtra("this", hashmap);
    startActivity(intent);
}
任何形式的帮助都将不胜感激。我似乎找不到任何方法来修复这个错误

编辑:不小心为dic写入了ArrayList而不是TreeMap。
添加自定义比较器。将for循环更改为在entryset上迭代。

尝试向另一个活动添加字符串属性:
public static final String EXTRA\u MESSAGE=“msg”。假设这些类在同一个包中,将额外的放在intent中,如下所示:
intent.putExtra(“AnotherActivity.extra_MESSAGE”,hashmap)
并从其他活动中检索意图,如下所示:
intent.getSerializableExtra(额外消息)

这将是另一项活动:

public class AnotherActivity extends ActionBarActivity {

public static final String EXTRA_MESSAGE="msg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_another);

    Intent intent = getIntent();
    HashMap<Long[], String> data = new HashMap<Long[], String>();
    data = (HashMap<Long[], String>) intent.getSerializableExtra(EXTRA_MESSAGE);
    ArrayList<Long[]> dic = new ArrayList<>();
    for (Long[] dat : data.keySet()) {
        dic.add(dat);
    }
}
public类另一个活动扩展了ActionBarActivity{
公共静态最终字符串EXTRA_MESSAGE=“msg”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_另一个);
Intent=getIntent();
HashMap数据=新的HashMap();
data=(HashMap)intent.getSerializableExtra(额外消息);
ArrayList dic=新的ArrayList();
for(Long[]dat:data.keySet()){
dic.add(dat);
}
}
这将是主要的活动:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(this, AnotherActivity.class);
    HashMap<Long[], String> hashmap = new HashMap<>();
    for (int i = 0; i < 10; i++) {
        hashmap.put(new Long[]{(long) i * 20, (long) i * 30}, "home" + i);
    }
    intent.putExtra(AnotherActivity.EXTRA_MESSAGE, hashmap);
    startActivity(intent);
}
公共类MainActivity扩展了ActionBarActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent Intent=新的Intent(this,AnotherActivity.class);
HashMap HashMap=新的HashMap();
对于(int i=0;i<10;i++){
hashmap.put(新的Long[]{(Long)i*20,(Long)i*30},“home”+i);
}
intent.putExtra(另一个activity.EXTRA_消息,hashmap);
星触觉(意向);
}
使用此循环(在
入口集上循环)而不是在
键集上循环

HashMap<Long[], String> data = new HashMap<Long[], String>();

for(Entry<Long[], String> entry : data.entrySet()) {
    Long[] key = entry.getKey();
    String value = entry.getValue();

    // do what you have to do here
}

第24行是什么?ArrayList dic=new ArrayList(data.keySet());它是for(Long[]dat:data.keySet())是的,而不是ArrayList dic=new ArrayList();for(Long[]dat:data.keySet()){dic.add(dat);}我真的很抱歉Mobeen,我不小心把dic写成了一个ArrayList,而实际上它是一个我试图添加数据的树状图。你的解决方案确实适用于ArrayList,如果dic实际上是一个树状图,我有没有办法使用这个解决方案?好的,所以我尝试使用额外的_消息,但它仍然会给我同样的错误。好的,所以我尝试通过e entry set并将其添加到树映射中:dic.put(dat.getKey(),dat.getValue());使用data:data.entrySet(),现在我得到一个ClassCastException:[Ljava.lang.Object;在dic.put(dat.getKey(),dat.getValue())上是不可比较的错误;我添加了一个自定义比较器,我得到了ClassCastException:java.lang.Object[]不能转换为java.lang.Long[]错误,但位于dic.put(dat,data.get(dat))@PinkyMystique将两种解决方案结合起来以使其工作。这是一个编辑,因此将编辑添加到第一个解决方案中,因此继续使用enrySet并使用自定义comparatorI。我们已将for循环更改为在entryset上交互,但我仍然会收到ClassCastException错误,除非现在将数据放入树映射中。
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(this, AnotherActivity.class);
    HashMap<Long[], String> hashmap = new HashMap<>();
    for (int i = 0; i < 10; i++) {
        hashmap.put(new Long[]{(long) i * 20, (long) i * 30}, "home" + i);
    }
    intent.putExtra(AnotherActivity.EXTRA_MESSAGE, hashmap);
    startActivity(intent);
}
HashMap<Long[], String> data = new HashMap<Long[], String>();

for(Entry<Long[], String> entry : data.entrySet()) {
    Long[] key = entry.getKey();
    String value = entry.getValue();

    // do what you have to do here
}
TreeMap<Long[], String> dic = new TreeMap<>(
    new Comparator<Long[]>() {
        public int compare(Long[] l1, Long[] l2) {
            // define your order
        }
    });