当我在我的一台笔记本电脑上的模拟器上运行我的应用程序时,它会为imageView.getDrawable()
返回null但是如果我在另一台笔记本电脑的模拟器上运行相同的应用程序/代码,它会运行而没有问题,如果我运行它一个实际的设备,它也可以毫无问题地运行。
此代码:
if ( !(imageView.getDrawable() instanceof BitmapDrawable)) return;
//Clean up to free memory
BitmapDrawable bitmapDrawable = (BitmapDrawable)imageView.getDrawable();
//THIS THROWS THE NULL POINTER (Which means the above line returned null)
bitmapDrawable.getBitmap().recycle();
抛出这个:
java.lang.NullPointerException
at com.bignerdranch.android.criminalintent.PictureUtils.cleanImageView(PictureUtils.java:54)
at com.bignerdranch.android.criminalintent.CrimeFragment.onStop(CrimeFragment.java:251)
at android.support.v4.app.Fragment.performStop(Fragment.java:1677)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:994)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
at android.support.v4.app.FragmentManagerImpl.dispatchStop(FragmentManager.java:1924)
at android.support.v4.app.FragmentActivity.onStop(FragmentActivity.java:613)
at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1212)
at android.app.Activity.performStop(Activity.java:5376)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3185)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3234)
at android.app.ActivityThread.access$1100(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我如何弄清楚为什么会在这台电脑上发生这种情况?这是计算机/设备:
Acer C720 Chromebook(Chrubuntu Linux),4GB RAM,Nexus 5仿真器 Nexus 9平板电脑
Samsung Series 9(openSUSE Linux),8GB RAM,Nexus 5 Emulator