我不想创建一个新的View对象,我已经在我的XML中定义了一个简单的View,我只想在它上面画一些条。
在XML中,我有一个空白的LinearLayout包含:
<View
android:id="@+id/bars"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey" />
然后是我必须处理的代码。在这个例子中,我只是通过绘制一个小的蓝色圆圈来测试它:
View bars = (View) this.findViewById(R.id.bars);
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
mPaint.setColor(Color.BLUE);
c.drawCircle(5, 5, 8, mPaint);
bars.draw(c);
当我尝试设置颜色时,我得到的错误是NullPointerException。如果我拿出那条线,那么当我尝试绘制圆圈时我会收到错误。我怎么能做我想做的事?
编辑 - 错误日志(我拿出了setColor):
05-19 00:56:14.100: E/AndroidRuntime(2422): FATAL EXCEPTION: main
05-19 00:56:14.100: E/AndroidRuntime(2422): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aheadstudios.talkalytics/com.aheadstudios.talkalytics.ViewStatsPrototype}: java.lang.NullPointerException
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.ActivityThread.access$600(ActivityThread.java:153)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.os.Looper.loop(Looper.java:137)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.ActivityThread.main(ActivityThread.java:5227)
05-19 00:56:14.100: E/AndroidRuntime(2422): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 00:56:14.100: E/AndroidRuntime(2422): at java.lang.reflect.Method.invoke(Method.java:511)
05-19 00:56:14.100: E/AndroidRuntime(2422): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-19 00:56:14.100: E/AndroidRuntime(2422): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-19 00:56:14.100: E/AndroidRuntime(2422): at dalvik.system.NativeStart.main(Native Method)
05-19 00:56:14.100: E/AndroidRuntime(2422): Caused by: java.lang.NullPointerException
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.graphics.Canvas.drawCircle(Canvas.java:961)
05-19 00:56:14.100: E/AndroidRuntime(2422): at com.aheadstudios.talkalytics.MultiStatsData.commonSetup(MultiStatsData.java:68)
05-19 00:56:14.100: E/AndroidRuntime(2422): at com.aheadstudios.talkalytics.MultiStatsData.<init>(MultiStatsData.java:50)
05-19 00:56:14.100: E/AndroidRuntime(2422): at com.aheadstudios.talkalytics.ViewStatsPrototype.setupMultiStatsData(ViewStatsPrototype.java:48)
05-19 00:56:14.100: E/AndroidRuntime(2422): at com.aheadstudios.talkalytics.ViewStatsPrototype.onCreate(ViewStatsPrototype.java:43)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.Activity.performCreate(Activity.java:5104)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-19 00:56:14.100: E/AndroidRuntime(2422): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
05-19 00:56:14.100: E/AndroidRuntime(2422): ... 11 more
答案 0 :(得分:0)
mPaint = new Paint();
然后代替bars.draw(c)
添加
bars.setBackgroundDrawable(new BackgroundDrawable(b));