通过intent传递可序列化导致ClassCastException错误

时间:2015-07-01 20:05:59

标签: java android android-intent

我传递一个HashMap,其中键值对Long []和String从一个活动传递到另一个活动,然后将HashMap数据放入TreeMap中,以便对其进行排序。为了传递数据,我做了一个HashMap数据的for循环。

以下是发送Intent的活动的代码:

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);
}

以下是接收意图的AnotherActivity类中的代码:

public class AnotherActivity extends ActionBarActivity {

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

    // Sorts the long values in TreeMap
    Comparator<Long[]> sortByTime = new Comparator<Long[]>() {
        @Override
        public int compare(Long[] one, Long[] two) {
            if (one[0] > two[0]) {
                return -1;
            } else if (one[0] < two[0]) {
                return 1;
            } else {
                return 0;
            }
        }
    };

    Intent intent = getIntent();
    HashMap<Long[], String> data = new HashMap<Long[], String>();
    data = (HashMap<Long[], String>) intent.getSerializableExtra("this");
    TreeMap<Long[], String> dic = new TreeMap<Long[], String>(sortByTime);
    for (Map.Entry<Long[], String> dat : data.entrySet()) {
        dic.put(dat.getKey(), dat.getValue());
    }
}

在dic.put(dat.getKey(),dat.getValue())中发生的ClassCastException错误; (第44行)和客户比较者开始(第26行):

Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.Long[]
        at com.mercurio.test.AnotherActivity$1.compare(AnotherActivity.java:26)
        at java.util.TreeMap.find(TreeMap.java:277)
        at java.util.TreeMap.putInternal(TreeMap.java:240)
        at java.util.TreeMap.put(TreeMap.java:186)
        at com.mercurio.test.AnotherActivity.onCreate(AnotherActivity.java:44)

任何形式的帮助将不胜感激。我似乎无法找到解决此错误的方法。

编辑:无意中编写了ArrayList而不是TreeMap for dic。 添加自定义比较器更改了for循环以迭代条目集。

2 个答案:

答案 0 :(得分:0)

尝试向AnotherActivity添加String属性:public static final String EXTRA_MESSAGE="msg";。假设这些类在同一个包中,将额外的内容添加到意图中:intent.putExtra("AnotherActivity.EXTRA_MESSAGE", hashmap);并从AntotherActivity中检索意图,如下所示:intent.getSerializableExtra(EXTRA_MESSAGE);

这将是AnotherActivity:

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);
    }
}

这将是MainActivity:

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);
}

答案 1 :(得分:0)

使用此循环(在entrySet上循环)而非循环keySet

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
}

编辑:树形图按排序顺序维护其键。数组没有定义任何排序,因此它不能直接用作键。您可以提供外部Comparator强加订单,但您必须定义对您有意义的订单:

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