Android 将实例/全局变量设置为 null 而不离开活动活动

发布于 2024-10-19 22:51:44 字数 2323 浏览 5 评论 0原文

目标平台版本 7
在许多设备上偶尔但定期发生。
可能在2.1中出现的频率更高
分析报告了错误,但随后的修订已将其范围缩小到一个特定问题。
我使用与此处找到的代码非常相似的代码创建对话框。

你好,我遇到了一个奇怪但非常严重的问题。在我的应用程序(游戏)中,我正在创建自定义对话框来通知用户各种事件。我使用 showDialog(int) 来执行此操作,而不是版本 8 中添加的 showDialog(int,Bundle)。这些对话框引用在活动中创建的对象以显示数据。调用 onCreateDialog 后,对象现在为空,导致设备强制关闭。我的假设是,触发对话框有点像触发活动,如果 Android 需要内存,它会清理调用活动的内存。

更多信息:

1:我的代码并没有将该对象设置为空,因为答:它在各处被引用,并且在创建对话框之前有几次机会抛出空指针。我还在调用 showDialog() 之前立即报告了该值。 B:它永远不会设置为 null,除非它没有默认值。 C:在异常期间尝试报告不为空的其他变量的值会发现它们也为空。例如:引用一个永远不为 null 且对 createDialog 没有用的字符串,表明它也为 null。但它也没有默认值。默认值对我没有帮助,因为对话框将不正确。

2:该应用程序使用了大量的内存,但我应该在一个舒适的范围内。

3:游戏中的某些对话更容易出现该问题,但除了主要的“最重”活动之外,其他活动中也出现过该问题,尽管这种情况非常罕见。

代码看起来大致像这样,但我正在解释流程,实际活动大约有一千行:

Class DataStructure {
    String ID;
    String Description;
    String name;
    boolean hasSpace;
    //initialize based on XML
}


Class Main extends Activity etc etc{<br>
DataStructure selectedData;



onClick{
    if(gameState == 1){
        selectedData = constructSelectedData();
        if(selectedData == null){
        //report error. Never happens.
        }
    }else if (gameState == 2){
    if(selectedData == null){
        //report error, never happens.
    }
     showDialog(5);
  }


onCreateDialog(int dialogDesired){

    if(dialogDesired == 5){
        if(selectedDialog.hasSpace){//NULL POINTER HERE REGARDLESS OF VARIABLES  REFERENCED.


    //set up dialog using typical builder model

   }

    }
}

编辑:堆栈跟踪

引起的:java.lang.NullPointerException 在 com.company.game.activity.GameMainMenuActivity.onCreateDialog(GameMainMenuActivity.java:1521) 在 android.app.Activity.onCreateDialog(Activity.java:2472) 在 android.app.Activity.createDialog(Activity.java:881) 在 android.app.Activity.restoreManagedDialogs(Activity.java:870) 在 android.app.Activity.performRestoreInstanceState(Activity.java:816) 在 android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)

关于堆栈跟踪的一些注释: 看起来它正在恢复活动,这很奇怪,因为它没有去任何地方。当对话框存在或要创建时,我可以通过主页按钮退出活动。事实上,作为 GameMainMenuActivity 的一部分,游戏会在恢复时重置。该错误也很常见,因此极不可能是用户在某个特定位置从主屏幕退出游戏的情况。我每天都会收到几十个这样的报告。

Targeting platform version 7
Occurs sporadically but regularly, on many devices.
Possible that it occurs more frequently in 2.1
Error is reported by analytics, but subsequent rev's have narrowed it down to one specific issue.
I am creating the dialogs using code very similar to what is found here.

Hi, I am having a strange but very serious problem. In my application (a game) I am creating custom dialogs to inform the user of various events. I am using showDialog(int) to do so, rather than showDialog(int,Bundle) added in version 8. These dialogs reference objects created in the activity in order to display data. Once onCreateDialog has been called, objects are now null resulting in a force-close on the device. My hypothesis is that triggering a dialog is somewhat like triggering an activity, if android needs memory it will clean up memory from the calling activity.

More info:

1: It is not the case that the object is being set to null by my code because A: It is referenced all over the place and has several opportunities to throw a null pointer before the dialog is created. I have also reported the value immediately before my call to showDialog(). B: It is never set to null save that it has no default value. C: Attempting to report the values of other variables that aren't null either during the exception reveals that they too are null. For example: Referencing a string that is never null that is not useful to createDialog reveals that it is null also. It too has no default value however. Default values won't help me though as the Dialogs would be incorrect then.

2: The app uses a considerable amount of memory, but I should be within limits by a comfortable amount.

3: Certain dialogs in the game are more prone to the problem, but it has occurred in other activities than the main 'heaviest' one albeit very rarely.

Code looks roughly like this, I'm paraphrasing for flow though, the actual activity is about a thousand lines:

Class DataStructure {
    String ID;
    String Description;
    String name;
    boolean hasSpace;
    //initialize based on XML
}


Class Main extends Activity etc etc{<br>
DataStructure selectedData;



onClick{
    if(gameState == 1){
        selectedData = constructSelectedData();
        if(selectedData == null){
        //report error. Never happens.
        }
    }else if (gameState == 2){
    if(selectedData == null){
        //report error, never happens.
    }
     showDialog(5);
  }


onCreateDialog(int dialogDesired){

    if(dialogDesired == 5){
        if(selectedDialog.hasSpace){//NULL POINTER HERE REGARDLESS OF VARIABLES  REFERENCED.


    //set up dialog using typical builder model

   }

    }
}

Edit: Stack trace

Caused by: java.lang.NullPointerException
at com.company.game.activity.GameMainMenuActivity.onCreateDialog(GameMainMenuActivity.java:1521)
at android.app.Activity.onCreateDialog(Activity.java:2472)
at android.app.Activity.createDialog(Activity.java:881)
at android.app.Activity.restoreManagedDialogs(Activity.java:870)
at android.app.Activity.performRestoreInstanceState(Activity.java:816)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)

Some notes about the stack trace: It seems like it is restoring the activity, which is odd because it hasn't gone anywhere. I can home button out of the activity while the dialog is there or to be created just fine. In fact as part of the GameMainMenuActivity, the game gets reset on restore. The bug is also common enough that it is extremely unlikely that it is a case of the user home-screening out of the game at some specific spot. I get dozens of these reported a day.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文