简体   繁体   English

按下主屏幕按钮但方向更改正常时,Android应用程序崩溃

[英]Android app crashes when home button is pressed but orientation change works fine

I'm developing an android application in which fragments use dynamically created views. 我正在开发一个Android应用程序,其中片段使用动态创建的视图。 Part of my onSaveInstanceState() method in my fragment is as follows: 我的片段中的onSaveInstanceState()方法的一部分如下:

outState.putSerializable("intToRow", (HashMap<Integer, View>) intToRow);

When I comment out this line everything works fine. 当我注释掉这一行时,一切正常。 I've left this line in for now and here's the problem: When I rotate the screen, this works fine and I can access it in onCreate() using 我暂时不在此行,这是问题所在:旋转屏幕时,它工作正常,可以使用onCreate()访问它

intToRow = (HashMap) savedInstanceState.getSerializable("intToRow");

And when the back button on the action bar is pressed the fragment and activity close without any problems. 当按下操作栏上的后退按钮时,片段和活动将关闭,没有任何问题。 So why is it that when I press the Home button or the multitasking button, the app crashes? 那么为什么当我按下“主页”按钮或多任务按钮时,应用程序崩溃了? The stack trace is: 堆栈跟踪为:

java.lang.RuntimeException: Parcel: unable to marshal value android.widget.TextView{383ee509 V.ED.... ........ 246,0-492,57 #3}
        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.app.FragmentState.writeToParcel(Fragment.java:147)
        at android.os.Parcel.writeTypedArray(Parcel.java:1191)
        at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:380)
        at android.os.Parcel.writeParcelable(Parcel.java:1357)
        at android.os.Parcel.writeValue(Parcel.java:1262)
        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.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3116)
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3695)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5832)
        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:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

I've seen many questions about the whole Parcel: unable to marshal value error but mine doesn't seem to match any of them. 我已经看到了很多有关整个Parcel: unable to marshal value错误,但我的似乎与它们都不匹配。 In my app, an orientation change and destroying the fragment seem to work perfectly but pressing the Home button makes it crash. 在我的应用程序中,方向更改和破坏片段似乎很正常,但是按下“主页”按钮会使它崩溃。 I've been through the Android lifecycle but I can't see anything that should happen when Home is pressed that wouldn't happen otherwise. 我已经经历了Android生命周期,但是当按下Home键时,我看不到会发生的其他任何事情。

This answer did get me in the right direction but the implementation was confusing. 这个答案确实使我朝着正确的方向前进,但是实现过程令人困惑。 The class could be 上课可能是

public class item { ... }

To resolve the marshal problem simply make the class serializable 要解决元帅问题,只需使类可序列化

public class item implements Serializable { .. } 

Hope this helps someone out... 希望这可以帮助某人...

I was struggling to find a solution to this problem too. 我也在努力寻找解决这个问题的方法。

My square and circle buttons caused the app to crash, while changing the orientation didn't cause it to crash. 我的方形和圆形按钮导致应用程序崩溃,而更改方向并没有导致应用程序崩溃。

I thought it must have been something to do with RecyclerView or Fragment, but I finally realized it was a Parcel problem. 我认为这一定与RecyclerView或Fragment有关,但我最终意识到这是一个包裹问题。

There don't seem to be any clear solutions to the problem (I searched and troubleshooted for over an hour), so here is the solution: 这个问题似乎没有任何明确的解决方案(我进行了一个多小时的搜索和故障排除),因此以下是解决方案:

The guy who asked the question showed his logcat which says 问这个问题的人显示了他的logcat,上面写着

Parcel: unable to marshal value ______

For him, the ______ is android.widget.TextView, but it will likely be something else for other people. 对他来说,______是android.widget.TextView,但对其他人来说可能是其他东西。

Whatever that class is, the problem is that it is being Parceled but is not implementing Parcelable. 无论该类是什么,问题都在于它正在被打包,但没有实现Parcelable。

So the solution is to implement Parcelable for that class. 因此,解决方案是为该类实现Parcelable。

I was confused because I had implemented Parcelable for the class I was Parceling, but I didn't know I also had to implement Parcelable for all the subclasses that would be Parceled with that main object class. 我很困惑,因为我已经为Parceling的类实现了Parcelable,但是我不知道我还必须为将与该主对象类一起打包的所有子类实现Parcelable。

This is how to implement Parcelable for the relevant class: 这是为相关类实现Parcelable的方法:

1) Check logcat and find where it says "unable to marshal value ______" (where ______ will be the relevant class for your problem. ) 1)检查logcat,并在其中显示“无法封送______的值”(其中______是与您的问题相关的类。

2) Go to the relevant class's .java file (if the issue is a default class or not your custom class, you will need to make a new class that extends that class, and then go to that class's .java file) 2)转到相关类的.java文件(如果问题是默认类或不是您的自定义类,则需要创建一个扩展该类的新类,然后转到该类的.java文件)

3) Left-click on the class name in the editor, and press alt+insert (to show the Generate menu), then click on Parcelable, then press OK 3)在编辑器中的类名上单击鼠标左键,然后按alt + insert(以显示Generate菜单),然后单击Parcelable,然后按OK。

4) Try to run your app again and reproduce the problem. 4)尝试再次运行您的应用并重现该问题。 If it happens again, check logcat again because you may have more than 1 class which need to implement Parcelable (I had 2 subclasses which needed to implement Parcelables), so check logcat each time and do the same thing for each class. 如果再次发生,请再次检查logcat,因为您可能有多个需要实现Parcelable的类(我有2个子类需要实现Parcelables),因此请每次检查logcat并对每个类执行相同的操作。

5) If you have multiple activities/fragments, try reproducing the problem on each page, so that you don't miss any other classes with the same problem. 5)如果您有多个活动/片段,请尝试在每个页面上重现该问题,以免您错过任何其他具有相同问题的课程。

That's it! 而已!

Cheers homies. 欢呼声。

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

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