簡體   English   中英

通過意圖傳遞可序列化導致ClassCastException錯誤

[英]Passing serializable through intent causing ClassCastException error

我將帶有鍵值對Long []和String的HashMap從一個活動傳遞到另一個活動,然后將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作為dic。 添加了自定義比較器。 更改了for循環以遍歷entryset。

嘗試將String屬性添加到AnotherActivity中: 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);
}

使用此循環(在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
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM