0

我在这里收到一个错误,上面写着,

at android.graphics.Bitmap_Delegate.nativeWidth(Bitmap_Delegate.java:291)
    at android.graphics.Bitmap.nativeWidth(Bitmap.java)
    at android.graphics.Bitmap.getWidth(Bitmap.java:785)

DDMS 说我在 ViewingWindow 类的第 66 行遇到错误,这是声明和初始化 int bitmapX 的位置:

protected void onDraw(Canvas canvas) {
        if (canvas == null) throw new NullPointerException();

        Bitmap bitmap = null;
        if (Monitor.getCurrent() == Monitor.TYPE.GREEN) bitmap = greenBitmap;
        else bitmap = redBitmap;

        int bitmapX = bitmap.getWidth() / 2;
        int bitmapY = bitmap.getHeight() / 2;

        int parentX = parentWidth / 2;
        int parentY = parentHeight / 2;

        int centerX = parentX - bitmapX;
        int centerY = parentY - bitmapY;

        matrix.reset();
        matrix.postTranslate(centerX, centerY);
        canvas.drawBitmap(bitmap, matrix, paint);
    }

为什么我会收到这样的错误?非常感谢任何帮助。谢谢。

这是greenBitmap和redBitmap的初始化变量:

private static Bitmap greenBitmap = null;
    private static Bitmap redBitmap = null;

    private static int parentWidth = 0;
    private static int parentHeight = 0;

    public ViewingWindow(Context context, AttributeSet attr) {
        super(context, attr);

        greenBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_icon);
        redBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_icon);
    }

    public ViewingWindow(Context context) {
        super(context);

        greenBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_icon);
        redBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_icon);
    }

来自 DDMS 的完整错误:

11-21 20:53:44.959: E/AndroidRuntime(521): FATAL EXCEPTION: main
11-21 20:53:44.959: E/AndroidRuntime(521): java.lang.NullPointerException
11-21 20:53:44.959: E/AndroidRuntime(521):  at com.ali.nazzal.sciencefairheartrateproject.ViewingWindow.onDraw(ViewingWindow.java:66)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.View.draw(View.java:6740)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.View.draw(View.java:6743)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
11-21 20:53:44.959: E/AndroidRuntime(521):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewRoot.draw(ViewRoot.java:1407)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.os.Looper.loop(Looper.java:123)
11-21 20:53:44.959: E/AndroidRuntime(521):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-21 20:53:44.959: E/AndroidRuntime(521):  at java.lang.reflect.Method.invokeNative(Native Method)
11-21 20:53:44.959: E/AndroidRuntime(521):  at java.lang.reflect.Method.invoke(Method.java:521)
11-21 20:53:44.959: E/AndroidRuntime(521):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-21 20:53:44.959: E/AndroidRuntime(521):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-21 20:53:44.959: E/AndroidRuntime(521):  at dalvik.system.NativeStart.main(Native Method)

----------------------------------------来自 DDMS 的新错误----------------- ---------------

  11-21 21:49:44.699: E/AndroidRuntime(551): FATAL EXCEPTION: main
    11-21 21:49:44.699: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ali.nazzal.sciencefairheartrateproject/com.ali.nazzal.sciencefairheartrateproject.Monitor}: android.view.InflateException: Binary XML file line #23: Error inflating class com.ali.nazzal.sciencefairheartrateproject.ViewingWindow
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.os.Looper.loop(Looper.java:123)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at java.lang.reflect.Method.invoke(Method.java:521)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at dalvik.system.NativeStart.main(Native Method)
    11-21 21:49:44.699: E/AndroidRuntime(551): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.ali.nazzal.sciencefairheartrateproject.ViewingWindow
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.createView(LayoutInflater.java:503)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.Activity.setContentView(Activity.java:1647)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at com.ali.nazzal.sciencefairheartrateproject.Monitor.onCreate(Monitor.java:60)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    11-21 21:49:44.699: E/AndroidRuntime(551):  ... 11 more
    11-21 21:49:44.699: E/AndroidRuntime(551): Caused by: java.lang.NoSuchMethodException: ViewingWindow(Context,AttributeSet)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at java.lang.Class.getMatchingConstructor(Class.java:660)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at java.lang.Class.getConstructor(Class.java:477)
    11-21 21:49:44.699: E/AndroidRuntime(551):  at android.view.LayoutInflater.createView(LayoutInflater.java:475)
    11-21 
21:49:44.699: E/AndroidRuntime(551):    ... 23 more
4

1 回答 1

1

将此构造函数添加到ViewingWindow:

public ViewingWindow(Context context, AttributeSet attrs, int defStyle) {
        super(context, attr, defStyle);

        greenBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green_icon);
        redBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_icon);
    }
于 2012-11-22T04:43:18.690 回答