ImageView causes crash but only when resuming from standby mode

自古美人都是妖i 提交于 2019-12-13 04:25:45

问题


I got an Activity class that uses two other classes; one of them extending ImageView and one that extends BaseAdapter like so:

My ImageView class does some progresses when it is called and when I am starting it for the first time it does what I want it to. This is also the case if I press home button and then resumes. But for some reason it does not work if I resume from standby mode. (pressing the button on the phone that makes the screen dark) When I unlock the phone it crashes. It crashes on a nullPointerException, and from what I can read when I try to call getChildAt() on a GridView defined by the BaseAdapter.

Is it any way to make the app do the same as when I press the home button, as when I set the phone in standby mode? EDIT:

public MyCustomDefinedImageViewClass (Context context) {
super (context);
mContext = context;
    MyCustomDefinedImageViewClass someThing =(MyCustomDefinedImageViewClass)gridViewIGetTheChildFrom.getChildAt(some int);

} This is what is causing my crash:

 11-19 22:00:48.200: E/AndroidRuntime(6252):     at my.package.name.MyCustomDefinedImageViewClass.<init>(MyCustomDefinedImageViewClass.java:57)

I do not think the code is helping to much and that was why I did not post it in the first place.


回答1:


Try adding

android:configChanges="keyboardHidden|orientation|screenLayout"

to your manifest file. The crash is a result of activity restart when the lock screen comes up. The lock screen forces portrait orientation and if you are not ready to handle it then your app may crash.

If that doesn't do it, try overriding onRetainNonConfigurationInstance().

See this post for info on how to retain objects: How to save lot of object in onRetainNonConfigurationInstance() in android

Also read this android-group thread for understanding what happens when device goes to sleep.



来源:https://stackoverflow.com/questions/13477336/imageview-causes-crash-but-only-when-resuming-from-standby-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!