简体   繁体   English

无法通过putExtra将哈希映射发送到新的活动

[英]Unable to send a Hash Map to a new Activity through putExtra

I am trying to send "HashMap pointMap" this hash map to a new activity , this is where i send the map , In oncreate method in Main2Activity class 我正在尝试将此哈希映射发送到“ 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);
        }
    });

And in here dv is a DrawingView class it's extends View and it has method call getPointMap it returns the hashMap 在这里dv是DrawingView类,它是View的扩展,它具有方法getPointMap,它返回hashMap

And this how i receive it from next activity 这就是我从下一个活动中收到的方式

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

before sending i checked whether it has any elements , in the log it shows it has elements but my problem is when the code runs it gives this exception 在发送之前,我检查了它是否具有任何元素,在日志中显示它具有元素,但是我的问题是代码运行时给出了此异常

FATAL EXCEPTION: main Process: com.example.chalaka.myapplication, PID: 22046 java.lang.RuntimeException: Parcel: unable to marshal value com.example.chalaka.myapplication.Point@293492bd at android.os.Parcel.writeValue(Parcel.java:1337) at android.os.Parcel.writeMapInternal(Parcel.java:614) at android.os.Parcel.writeMap(Parcel.java:598) at android.os.Parcel.writeValue(Parcel.java:1255) at android.os.Parcel.writeArrayMapInternal(Parcel.java:638) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313) at android.os.Bundle.writeToParcel(Bundle.java:1096) at android.os.Parcel.writeBundle(Parcel.java:663) at android.content.Intent.writeToParcel(Intent.java:7335) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2485) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1486) at android.app.Activity.startActivityForResult(Activity.java:3796) at android.app.Activity.startActivityForResult(Activity.java:3744) at android.support.v4.app.Fragme 致命异常:主进程:com.example.chalaka.myapplication,PID:22046 java.lang.RuntimeException:包裹:无法将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。 android.content.Intent.writeToParcel(Intent.java:7335)位于android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2485)位于Parcel.writeBundle(Parcel.java:663)在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.Fragme ntActivity.startActivityForResult(FragmentActivity.java:784) at android.app.Activity.startActivity(Activity.java:4067) at android.app.Activity.startActivity(Activity.java:4035) at com.example.chalaka.myapplication.Main2Activity$1.onClick(Main2Activity.java:93) at android.view.View.performClick(View.java:4808) at android.view.View$PerformClick.run(View.java:19918) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5536) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192) com.example.chalaka.myapplication.Main2Activity处的android.app.Activity.startActivity(Activity.java:4067)处的ntActivity.startActivityForResult(FragmentActivity.java:784)在android.app.Activity.startActivity(Activity.java:4035)处$ 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),位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java.lang.reflect.Method.invoke(Method.java:372)处的java.lang.reflect.Method.invoke(Method.java:372)处。 java:1397),位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)

please help me to solve this... 请帮我解决这个问题...

The problem is that your class com.example.chalaka.myapplication.Point does not implement Parcelable nor Serializable . 问题在于您的com.example.chalaka.myapplication.Point类未实现ParcelableSerializable

Do so and the code above will work as you expect. 这样做,上面的代码将按预期工作。

Write this method from where you want to start new Activity: 从您要开始新活动的位置编写此方法:

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

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

}

And to get this map : 并获得此地图:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM