Android:android.graphics.Canvas.drawLine(Canvas.java:809)中的java.lang.NullPointerException(可能与我的drawable资源有关)

时间:2011-01-26 22:17:20

标签: android nullpointerexception drawable

我正在尝试在每列中构建一个包含7列和24个TextView的子视图。

当我的活动开始时,我得到一个异常,它在堆栈跟踪中的任何地方都没有我的包。

以下是24个文本视图中每个视图的背景可绘制资源

<?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>

在发生这种情况之前,CPU会持续一两秒钟。

我正在使用Android 2.2的模拟器

这是我的例外。

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.handleMessage(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)

修改

将大小重命名为笔划可以消除崩溃,但我没有得到虚线。我想要在drawable的中间或底部有一条水平虚线。

1 个答案:

答案 0 :(得分:7)

发生崩溃是因为GradientDrawable.mStrokePaint为null,而这又是因为您没有在XML中指定<stroke>

<size>更改为<stroke>会修复它,但您还需要将笔触颜色更改为与背景颜色不同,即:

<?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>