Android: java.lang.NullPointerException at android.graphics.Canvas.drawLine(Canvas.java:809) (probably related to my drawable resource)
I am trying to build a child view with 7 columns and 24 TextViews in each column.
I get an exception when my activity starts and it does not have my package anywhere in the stack trace.
Here is my background drawable resource for each of the 24 text views
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape><solid android:color="#CCC"/></shape></item>
<item android:top=".5pt" android:bottom=".5pt">
<shape android:shape="line">
<size android:height="1pt" android:color="#CCC" android:dashWidth="1pt" android:dashGap="1pt" />
</shape>
</item>
</layer-list>
The CPU goes to 100% for a second or two before this happens.
I am using the emulator for Android 2.2
Here is my exception.
01-26 16:51:38.084: ERROR/AndroidRuntime(955): FATAL EXCEPTION: main
01-26 16:51:38.084: ERROR/AndroidRuntime(955): java.lang.NullPointerException
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.graphics.Canvas.drawLine(Canvas.java:809)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:347)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.graphics.drawable.LayerDrawable.draw(LayerDrawable.java:320)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.View.draw(View.java:6725)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.View.draw(View.java:6846)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.View.buildDrawingCache(View.java:6502)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.onAnimationStart(ViewGroup.java:1253)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1499)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.View.draw(View.java:6743)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.View.draw(View.java:6743)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.View.draw(View.java:6743)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewRoot.draw(ViewRoot.java:1407)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.view.ViewRoot.handle开发者_StackOverflow中文版Message(ViewRoot.java:1727)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.os.Looper.loop(Looper.java:123)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at java.lang.reflect.Method.invoke(Method.java:521)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-26 16:51:38.084: ERROR/AndroidRuntime(955): at dalvik.system.NativeStart.main(Native Method)
Edit
Renaming size to stroke eliminates the crash but I do not get a dotted line. I want a horizontal dotted line either in the middle or on the bottom of the drawable.
The crash occurs because GradientDrawable.mStrokePaint is null, which is in turn because you did not specify a <stroke>
in your XML.
Changing <size>
to <stroke>
will fix it, but you also need to change the stroke colour to be different to the background colour, i.e:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape><solid android:color="#CCC"/></shape></item>
<item android:top=".5pt" android:bottom=".5pt">
<shape android:shape="line">
<stroke android:height="1pt" android:color="#FF000000" android:dashWidth="1pt" android:dashGap="1pt" />
</shape>
</item>
</layer-list>
精彩评论