简体   繁体   English

当内存加速器杀死我的应用程序时,它会强制关闭

[英]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. 该应用程序可以正常工作,直到我尝试使用称为Memory Booster的应用程序将其杀死。 Then it force closes and my logcat throws 2 NULLPOINTEREXCEPTIONS. 然后它强制关闭,我的logcat抛出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 ). 在我的textview(在mainactivity中声明并传递给广播接收器)和ConnectiviyManager(也在main activity中声明,但在Broadcast接收器中使用)。

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) 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: 第122行是:

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

If i comment out line 121, i get NULLPOINTEREXCEPTION ON NEXT LINE which is: 如果我注释掉第121行,则会在下一行得到NULLPOINTEREXCEPTION,即:

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). 您可以做什么:覆盖主要活动的onDestroy,然后在那里正确关闭所有内容(您的服务,广播等)。 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. conMannull因为在您运行的任何代码路径中,您都没有对其进行初始化。 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. 那是因为从2.2 Android版本起,您无法为此目的与其他应用程序完全杀死该应用程序。 Android just wont allow that. Android只是不允许这样做。

  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. 当任务管理器或任何应用“杀死”我的应用时,并且该应用具有正在运行的服务(Android操作系统)希望它们再次运行,因此它将再次开始运行服务。

  3. Because activity is DEAD and there i instantiated my objects they cannot be used in service again. 因为活动是DEAD,所以我实例化了我的对象,因此它们无法再次用于服务中。 Some objects MUST be instantiated in service class which i made. 有些对象必须在我创建的服务类中实例化。 ( so they are not null ) (因此它们不为null)

  4. So i made an instance of WifiManager and other important objects in my custom service class. 因此,我在自定义服务类中创建了WifiManager和其他重要对象的实例。 When OS starts Service again , they are instantiated and ready on use (They are not null ) 当OS再次启动Service时,它们被实例化并可以使用(它们不为null)

Sorry for my bad english .. hope you understand what i tried to say :) 对不起,我的英语不好..希望您能理解我的意思:)

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

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