无法通过putExtra

时间:2016-05-31 10:08:51

标签: java android

我正在尝试发送" HashMap pointMap"这个哈希映射到一个新的活动, 这是我发送地图的地方, 在Main2Activity类中的oncreate方法

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(Main2Activity.this, OutActivity.class);

            Log.d("Null",dv.getPointMap().toString());

            myIntent.putExtra("hashMap", dv.getPointMap());
            //pass input int to next activity
            Main2Activity.this.startActivity(myIntent);
        }
    });

在这里dv是一个DrawingView类,它扩展了View并且它有方法调用getPointMap它返回hashMap

这就是我从下一个活动中收到它的方式

    hashMap = (HashMap<Integer , Point>) this.getIntent().getSerializableExtra("hashMap");

在发送之前我检查它是否有任何元素,在日志中它显示它有元素  但我的问题是当代码运行时它会给出这个异常

  

致命的例外:主要                                                                                      处理:com.example.chalaka.myapplication,PID:22046                                                                                      java.lang.RuntimeException:Parcel:无法编组值   com.example.chalaka.myapplication.Point@293492bd                                                                                          在android.os.Parcel.writeValue(Parcel.java:1337)                                                                                          在android.os.Parcel.writeMapInternal(Parcel.java:614)                                                                                          在android.os.Parcel.writeMap(Parcel.java:598)                                                                                          在android.os.Parcel.writeValue(Parcel.java:1255)                                                                                          在android.os.Parcel.writeArrayMapInternal(Parcel.java:638)                                                                                          在android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)                                                                                          在android.os.Bundle.writeToParcel(Bundle.java:1096)                                                                                          在android.os.Parcel.writeBundle(Parcel.java:663)                                                                                          在android.content.Intent.writeToParcel(Intent.java:7335)                                                                                          在   android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2485)                                                                                          在   android.app.Instrumentation.execStartActivity(Instrumentation.java:1486)                                                                                          在android.app.Activity.startActivityForResult(Activity.java:3796)                                                                                          在android.app.Activity.startActivityForResult(Activity.java:3744)                                                                                          在   android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)                                                                                          在android.app.Activity.startActivity(Activity.java:4067)                                                                                          在android.app.Activity.startActivity(Activity.java:4035)                                                                                          在   com.example.chalaka.myapplication.Main2Activity $ 1.onClick(Main2Activity.java:93)                                                                                          在android.view.View.performClick(View.java:4808)                                                                                          在android.view.View $ PerformClick.run(View.java:19918)                                                                                          在android.os.Handler.handleCallback(Handler.java:739)                                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                          在android.os.Looper.loop(Looper.java:135)                                                                                          在android.app.ActivityThread.main(ActivityThread.java:5536)                                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                                          在java.lang.reflect.Method.invoke(Method.java:372)                                                                                          在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1397)                                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)

请帮我解决这个问题......

4 个答案:

答案 0 :(得分:2)

问题是,您的班级com.example.chalaka.myapplication.Point未实施ParcelableSerializable

这样做,上面的代码将按预期工作。

答案 1 :(得分:0)

从您想要开始新活动的地方写下此方法:

private void gotoServiceFragment(HashMap<String, String> map) {

 Intent gotoService = new Intent(registration.this, Service.class);
 gotoService.putExtra(AndyConstants.MAP, map);
 startActivity(gotoService);

}

要获得地图:

 Bundle bundle = getIntent().getExtras();
 HashMap<String, String> map = (HashMap<String, String>) bundle.getSerializable(AndyConstants.MAP);

答案 2 :(得分:0)

尝试在com.example.chalaka.myapplication.Point类中实现Parcelable

答案 3 :(得分:0)

For example, when sending the intent:

HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);

in MyOtherActivity activity

Intent intent = getIntent();    
HashMap<String, String> hashMap = (HashMap<String, String>) intent.getSerializableExtra("map");