简体   繁体   中英

When memory booster kills my app, it force closes

I've programmed an app which have Main activity, Service and Broadcast receiver. The app, works until i'm trying to kill it with app called memory booster. Then it force closes and my logcat throws 2 NULLPOINTEREXCEPTIONS. On my textview (declared in mainactivity and passed to broadcast receiver ), and ConnectiviyManager ( declared in main activity also, but using in Broadcast receiver ).

01-24 16:25:06.790: E/AndroidRuntime(4820): java.lang.RuntimeException: Error receiving broadcast Intent { act=ACTION_PERFORMED (has extras) } in app.wifito3g.Broadcast@40628910
01-24 16:25:06.790: E/AndroidRuntime(4820): Caused by: java.lang.NullPointerException

01-24 16:25:06.790: E/AndroidRuntime(4820): at app.wifito3g.Broadcast.onReceive(Broadcast.java:122)

Can anyone explains me why ? Thanks!

EDIT: Detailed stacktrace

在此处输入图片说明

and line 122 is:

wifi_state = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

If i comment out line 121, i get NULLPOINTEREXCEPTION ON NEXT LINE which is:

wifi_check.setTextColor(Color.BLACK);

What you can do: override the onDestroy of your main activity, and there, close everything properly (your services, broadcast etc). In this case, you wil not get a force close anymore.

conMan is null because, in whatever code path you ran down, you have not initialized it. Determine a better place to initialize that value that covers this case.

Ok i figured out what was wrong. Here is the solution.

  1. When task manager or whatever app, kills my app it force closes. Thats because from 2.2 Android version you cannot FULLY kill app with other app for that purpose. Android just wont allow that.

  2. When task manager or whatever app "kills" my app, and this app has a running services, an android OS, wants them to run again, so it starts running services again.

  3. Because activity is DEAD and there i instantiated my objects they cannot be used in service again. Some objects MUST be instantiated in service class which i made. ( so they are not null )

  4. So i made an instance of WifiManager and other important objects in my custom service class. When OS starts Service again , they are instantiated and ready on use (They are not null )

Sorry for my bad english .. hope you understand what i tried to say :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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