那么问题是,我有一个在主类里面有onConfigurationChanged的livewallpaper,
代码是
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);
}
}
}
它运行良好,当手机处于开机状态时,但如果手机重新启动,则会发生崩溃
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)
在我的手机 Nexus 4 和三星 Galaxy S2 上运行良好,手机重启时不会崩溃,但在其他一些设备上会崩溃。
这也发生在 LG-P990 上,我认为,当手机重新启动时,它没有方向信息,但我不知道为什么调用 onConfigurationChanged?