开发者

Why is this an NPE?

Why do I get an NPE at Drawable pic = (Drawable) i.getDrawable();?????? I am trying to get the height and width of a drawable in order to set its scale to be fit the XY of an imageview in an imageswitcher via a matrix (using a matrix to enable multitouch).

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);

Error Log:

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


getDrawable() is almost certainly returning a null value. Which isn't a problem in itself, but then you get an NPE when you try to cast that null value to a Drawable.

The underlying problem is that a newly created ImageView won't have a drawable associated with it yet (as is evidenced by the fact that it returns null)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜