开发者

Android sets instance/global variables to null without leaving active activity

开发者 https://www.devze.com 2023-02-14 08:52 出处:网络
Targeting platform version 7 Occurs sporadically but regularly, on many devices. Possible that it occurs more frequently in 2.1

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消