putExtra treeMap returns HashMap cannot be cast to TreeMap android

前端 未结 3 1209
星月不相逢
星月不相逢 2020-12-11 04:28

I need your help, I cannot understand what\'s happening?

I\'m trying to send a TreeMap between 2 activities, the code is something like this:

class O         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 04:31

    As alternative to @Jave's suggestions, if you really need the data structure to be a TreeMap, just use the appropriate constructor that takes another map as data source. So on the receiving end (Two) do something like:

    public class Two extends Activity {
        @Override public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TreeMap map = new TreeMap((Map) getIntent().getExtras().get("map"));
        }
    }
    

    However, depending on your project, you probably don't have to worry about the exact Map implementation. So in stead, you could just cast to the Map interface:

    public class Two extends Activity {
        @Override public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Map map = (Map) getIntent().getExtras().get("map");
        }
    }
    

提交回复
热议问题