问题与全局变量 [英] Issue with global variables

查看:71
本文介绍了问题与全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过扩展应用程序在我的应用程序中定义了一些全局变量,如下所示。当我离开应用程序时,打开更多应用程序并与他们玩一下,然后回到我的应用程序,全局变量已被删除,我的应用程序崩溃。我有两个问题:

1-这怎么可能?

2-我怎样才能强迫我的应用在退出时退出?我知道我不应该这样做,但我找不到其他解决方案...



谢谢

  public class GlobalVars extends Application {

public static HashMap< Integer,String> ID2Cat = new HashMap< Integer,String>();

//用户记录的bool
public static boolean isLogged = false;

//当前菜单项
public static int currentMenuItem = 0;
public static boolean isHome = false;

// Goodideas
public static JSONObject goodIdeas = new JSONObject();

// Meteo
public static JSONArray weatherItems = new JSONArray();

//更多东西

}

Logcat



这让我觉得我的应用程序被杀死了。这是显示在某些时候与其他应用程序玩。

 我/ ActivityManager(2465):进程com.mysite.myapp pid 23538)已经死亡。 
I / WindowManager(2465):WIN DEATH:Window {4852a678 com.mysite.myapp / com.mysite.myapp.Home paused = false}
I / WindowManager(2465) 485b63a8 com.mysite.myapp / com.mysite.myapp.Home paused = false}
I / WindowManager(2465):WIN DEATH:窗口{4826fbf8 com.mysite.myapp / com.mysite.myapp.ItemList paused = false}
I / WindowManager(2465):WIN DEATH:Window {48286f90 com.mysite.myapp / com.mysite.myapp.ItemDetail paused = false}
W / GpsLocationProvider(2465):Unneeded remove listener对于uid 1000
D / GpsLocationProvider(2465):stopNavigating
D / gps_BRCM(2465):[status check] on_stop():GPS_STATUS_SESSION_END
D / gps_BRCM(2465):gps_engine_status_update 2
D / GpsLocationProvider(2465):发送意图通知已启用或禁用GPS
D / gps_BRCM(2465):gps_stop:调用
V / GpsLocationProvider(2465):hybridGpsSensorDeregister:No注册的sensorManager
D / GpsLocationProvider(2465):hybridGpsSensorDeregister


解决方案


1-这怎么可能?

$ b

您的进程可能会在任何时候终止。你不能假定任何静态数据成员或自定义的 Application 对象将存在多久。他们应该只能用作缓存,最好。


2-如何强制我的应用程序在退出时退出?


您不需要。


I知道我不应该这样做


不仅如此,在这种情况下它也无济于事。所有它会做的是意味着你100%的时间崩溃,因为你实际上并没有打扰到正确初始化。


但是我找不到其他解决方案。


初始化您的数据。如果您的应用程序由于创建了新的自定义 Application 实例而崩溃,则您的 Application 子类会有错误,您需要修复。


I defined some global variables in my app by extending Application, as shown below. When I leave the app, open more apps and play a bit with them, and go back to my app, the global variables have been deleted and my app crashes. I've got 2 questions:

1- How can this be possible?

2- How can I force my app to exit when going to background? I know I'm not supposed to do it, but I can't find other solution...

Thanks

public class GlobalVars extends Application { 

    public static HashMap<Integer, String> ID2Cat = new HashMap<Integer, String>();

    // User logged bool
    public static boolean isLogged = false;

    // Current menu item
    public static int currentMenuItem = 0;
    public static boolean isHome = false;

    // Goodideas    
    public static JSONObject goodIdeas = new JSONObject();

    // Meteo
    public static JSONArray weatherItems = new JSONArray();

    // More stuff

}

Logcat

Here's what makes me think my app is killed. This is shown at some point while playing with other apps.

I/ActivityManager( 2465): Process com.mysite.myapp (pid 23538) has died.
I/WindowManager( 2465): WIN DEATH: Window{4852a678 com.mysite.myapp/com.mysite.myapp.Home paused=false}
I/WindowManager( 2465): WIN DEATH: Window{485b63a8 com.mysite.myapp/com.mysite.myapp.Home paused=false}
I/WindowManager( 2465): WIN DEATH: Window{4826fbf8 com.mysite.myapp/com.mysite.myapp.ItemList paused=false}
I/WindowManager( 2465): WIN DEATH: Window{48286f90 com.mysite.myapp/com.mysite.myapp.ItemDetail paused=false}
W/GpsLocationProvider( 2465): Unneeded remove listener for uid 1000
D/GpsLocationProvider( 2465): stopNavigating
D/gps_BRCM( 2465): [status check] on_stop() : GPS_STATUS_SESSION_END
D/gps_BRCM( 2465): gps_engine_status_update 2
D/GpsLocationProvider( 2465): send an intent to notify that the GPS has been enabled or disabled
D/gps_BRCM( 2465): gps_stop: called
V/GpsLocationProvider( 2465): hybridGpsSensorDeregister : No registered sensorManager
D/GpsLocationProvider( 2465): hybridGpsSensorDeregister

解决方案

1- How can this be possible?

Your process may be terminated at any point. You cannot assume how long any static data members or custom Application objects will live. They should only be used as a cache, at best.

2- How can I force my app to exit when going to background?

You don't.

I know I'm not supposed to do it

Not only that, but it will not help you in this case. All it will do is mean that you crash 100% of the time, since you didn't actually bother to initialize properly.

but I can't find other solution.

Initialize your data. If your application is crashing because a new custom Application instance was created, your Application subclass has bugs, which you need to fix.

这篇关于问题与全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆