我這裏得到一個錯誤,說,如何解決位圖刪除中的NullPointerException。
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類,這是在INT bitmapX聲明和初始化線66得到一個錯誤:
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
什麼是'位圖'在那一點? 'greenBitmap'或'redBitmap'?其中之一就是'null'。 – arshajii
問題不在於'bitmap',而是'greeenBitmap'或'redBitmap'爲空。確保它們不爲空。 –
但它似乎堆棧跟蹤說空指針發生在android.graphics.Bitmap_Delegate.nativeWidth(Bitmap_Delegate.java:291)不在ViewingWindow:66 ...你可以發佈完整的錯誤堆棧跟蹤嗎? –