全局变量变成空有时 [英] Global variable becomes null sometimes

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

问题描述

我有一个扩展应用在我的基地基地声明的变量。

变量是静态的,我能够把它和访问它就好了。但是,在一些设备上是空的时候。

 公共类应用扩展android.app.Application {公共静态的ArrayList<串GT;下一页;
@覆盖
公共无效的onCreate(){
    super.onCreate();
      ...
    }
}

 <应用
            机器人:名字=应用程序
            机器人:allowBackup =真
            机器人:图标=@绘制/ ic_launcher
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@风格/ Theme.MyTheme>
      ...

我引用这样的: Application.nextPage

我使用的错误意识,我看到已经有30个错误在哪里这个变量为空。它只是很少发生,因为应用程序变得每天数千用户和该错误只发生在本周30倍。

是任何人都知道这样一个问题,或者什么可能造成的呢?

问题的设备:

  SCH-I605 19%3
SM-N900P 13%2
ST26i 13%2
SAMSUNG-SGH-I337 13%2
SHV-E210S 13%2
DROID RAZR HD 13%2
SCH-I200 13%2
DROID RAZR 7%1

发生下列问题SDK:

  4.1.2 40%12
4.3 30%9
4.4.2 10%3
4.1.1 7%2
4.2.2 7%2
4.0.3 4%1
2.3.6 4%1


解决方案

凡在你的应用程序,你初始化下一页?

我会建议加入下一页=新的ArrayList<弦乐>(); 在应用程序中的onCreate,或者干脆直接在现场变量

如果您在活动初始​​化下一页,然后空的一个可能的方案可能是:


  • 应用启动

  • 活动启动 - 下一页设置有效

  • 用户离开应用程序

  • 的Andr​​oid应用程序破坏(可能是一段时间后)

  • 广播发送(如果你定义了收听广播清单中)

  • 应用程序已启动(但不活动)

  • 的下一页列表为空

不知道你的应用程序是如何工作的,这是一种思想的例子,但可能是你的问题的指示。

I have a variable declared in my base base that extends Application.

The variable is static and i am able to set it and access it just fine. However on some devices it is null sometimes.

public class Application extends android.app.Application {

public static ArrayList<String> nextPage;


@Override
public void onCreate() {
    super.onCreate();
      ...
    }
}

 

<application
            android:name="Application"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.MyTheme" >
      ...

I am referencing it like this: Application.nextPage

I am using bug sense and I see there have been 30 errors where this variable is null. It only happens rarely, because the app gets thousands of users per day and this error has only occured 30 times this week.

Is anyone aware of such an issue or what might be causing it?

Problem devices:

SCH-I605 19%     3
SM-N900P 13%     2
ST26i 13%        2
SAMSUNG-SGH-I337 13% 2
SHV-E210S 13%    2
DROID RAZR HD 13%    2
SCH-I200 13%     2
DROID RAZR 7%    1

Problem occurred on the following SDK:

4.1.2 40%    12
4.3 30%      9
4.4.2 10%    3
4.1.1 7%     2
4.2.2 7%     2
4.0.3 4%     1
2.3.6 4%     1

解决方案

Where in your app do you initialize nextPage?

I would recommend adding nextPage = new ArrayList<String>(); in onCreate of Application, or simply directly in the field variable.

If you initialize nextPage in an activity, then a possible scenario of null could be:

  • App starts
  • Activity starts - nextPage is set and valid
  • User leaves the App
  • Android destroys App (possibly after some time)
  • A broadcast is sent (if you have defined broadcast listening in manifest)
  • App is started (but not Activity)
  • The nextPage list is null

Not knowing how your app is working, this is a thought example but could be an indication of your problem.

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

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