爲什麼我在可繪製的圖片上得到NPE =(Drawable)i.getDrawable(); ?????? 我想獲取繪圖的高度和寬度,以便通過矩陣(使用矩陣啓用多點觸控)將其比例尺設置爲適合圖像查看器在圖像查看器中的XY。爲什麼這是NPE?
public View makeView() {
ImageView i = new ImageView(this);
Drawable pic = (Drawable) i.getDrawable();
float displayHeight = i.getHeight();
float imageHeight = pic.getIntrinsicHeight();
float displayWidth = i.getWidth();
float imageWidth = pic.getIntrinsicWidth();
float scaleX = (float) displayWidth/(float) imageWidth;
float scaleY = (float) displayHeight/(float) imageHeight;
matrix.setScale(scaleX, scaleY);
i.setBackgroundColor(0xFF000000);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
i.setImageMatrix(matrix);
i.setScaleType(ScaleType.MATRIX);
錯誤日誌:
E/AndroidRuntime(22016): FATAL EXCEPTION: main
E/AndroidRuntime(22016): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ben.test/com.ben.test.ImageSwitch1}: java.lang.NullPointerException
E/AndroidRuntime(22016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime(22016): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(22016): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E /AndroidRuntime(22016): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(22016): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(22016): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(22016): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(22016): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(22016): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(22016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(22016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(22016): at dalvik.system.NativeStart.main(Native Method)
E /AndroidRuntime(22016): Caused by: java.lang.NullPointerException
E/AndroidRuntime(22016): at com.ben.test.ImageSwitch1.makeView(ImageSwitch1.java:185)
E/AndroidRuntime(22016): at android.widget.ViewSwitcher.obtainView(ViewSwitcher.java:80)
E/AndroidRuntime(22016): at android.widget.ViewSwitcher.setFactory(ViewSwitcher.java:99)
E/AndroidRuntime(22016): at com.ben.test.ImageSwitch1.onCreate(ImageSwitch1.java:100)
E/AndroidRuntime(22016): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(22016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime(22016): ... 11 more
W/ActivityManager(1106): Force finishing activity com.ben.test/.ImageSwitch1
W/ActivityManager(1106): Force finishing activity com.ben.test/.LP
請出示NPE的堆棧跟蹤(這是由事實,即它返回null證明)。 –
我認爲這就是你要找的,讓我知道你是否想要其他數據... – benbeel
如果你還*告訴我們這是你的代碼的第185行... –