0

Well the problem is, i have livewallpaper that has inside main class a onConfigurationChanged,

the code is

 public void onConfigurationChanged (Configuration newConfig){
       if(MODE == 0) {   
        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
            {
                    scene.setScale(1); //this is line 920
                    scene.setPosition(0, 0);
            }
            else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                    scene.setScaleY(1.6f);
                    scene.setScaleX(0.6f); 

                    scene.setPosition(120, -240);
            }
       }
       else if (MODE == 1)
       {
            if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
            {
                 scene.setScaleY(0.6f);
                 scene.setScaleX(1.6f);



                 scene.setPosition(-240, 120);
            }
            else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                 scene.setScale(1);
                 scene.setPosition(0, 0);
            }
       }
    }

It works well, while phone is being ON, but if phone gets restarted that crash occurs

java.lang.NullPointerException
at org.example.example.examplelw.onConfigurationChanged(examplelw.java:920)
at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3478)
at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:3602)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)

On my Phones Nexus 4 and Samsung Galaxy S2 works well, and does not crash when phone is restarted, but it crash on some other devices.

This happens on LG-P990 too, what i think is, that when phone is getting restarted it does not have Orientation info, but i dont know why onConfigurationChanged is called?

Allen
  • 65
  • 9
  • Why not check newConfig for null pointer before doing stuff? – Yaroslav Mytkalyk Jan 30 '13 at 18:23
  • 1
    If you want help, you at least need to post the code that is causing the error, taking it out and just putting a comment there is not helpful. Specifically, what are you doing on line 920 of diablo3lw.java? – iagreen Jan 30 '13 at 18:23
  • It looks like scene as a nullpointer, onConfigurationChanged is called before the scene is created. Wierd. I commented here a line 920 – Allen Jan 30 '13 at 18:36
  • Yeah, it is getting called and your member variable is not initialized. Not sure why, but you can guard against it by checking it is null. – iagreen Jan 30 '13 at 18:56

3 Answers3

0

As I know problem is that object scene in this scene.setScale(1); is null. You are using null value so java thowing NULLPoinerException.

To solve this problem where this object is initialize. I think its not initialize so it contain null so you will get your solution using debug the application.

If this method is called before scene is created you can do like this.

if(scene == null) {
  // Initialize the scene object.
}

then rest of the code.

OR

if(scene != null) {
    then rest of the code. 
}
Ajay S
  • 45,716
  • 27
  • 84
  • 103
0

Have you tried to save scene object? If it is parcelable od serializable you can save it using onSaveInstanceState() method and in your onCreate() you can restore it. More details here.

Community
  • 1
  • 1
Michał Z.
  • 4,069
  • 1
  • 20
  • 32
0

is this ok?

if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
            {
                if (scene == null)
                {
                    //do nothing
                }
                else
                    scene.setScale(1);
                    scene.setPosition(0, 0);
            }
Allen
  • 65
  • 9