场景如下:
我有一个活动跑步训练锻炼查看使用 XML 布局 _run_workout.xml_ 以及一些更新的标签倒计时器。工作正常...
现在,除了每秒更新的标签之外onTick()CountDownTimer 对象的回调方法 我想将自定义表面视图添加到我的 _run_workout.xml 布局_ 中,该视图将绘制一些由相同更新的弧线onTick()每秒方法...
我的 run_workout.xml:
<training.timer.CounterClockView
android:id="@+id/counter_clock_surface"
android:layout_width="300dp"
android:layout_height="240dp">
</training.timer.CounterClockView>
我的自定义视图扩展表面视图
public class CounterClockView extends SurfaceView {
Paint paint = new Paint();
Paint paint2 = new Paint();
final RectF rect = new RectF();
final RectF rect2 = new RectF();
int counterArcAngle = 15;
//constructor
public CounterClockView(Context context, AttributeSet attributeSet) {
super(context);
//setting some paint properties...
this.setBackgroundColor(Color.TRANSPARENT);
}
@Override
public void onDraw(Canvas canvas) {
rect.set(50, 50, 150, 150);
rect2.set(50, 50, 150, 150);
this.layout(0, 0, 200, 200);
canvas.drawArc(rect, -90, 360, false, paint);
canvas.drawArc(rect2, -90, counterArcAngle, false, paint2);
}
我扩展活动的主要类是获取对自定义的引用表面视图在布局中使用以下代码:
//counterClockView is declared outside of onCreate() as CounterClockView counterClockView;
//later in onCreate(){....
counterClockView = (CounterClockView) findViewById(R.id.counter_clock_surface);
问题是更改customView对象(counterClockView)的成员变量的值
counterClockView.counterArcAngle = 10;
会使应用程序崩溃...
另外,从我的主要活动中,我想打电话给无效()更改后重做表面视图的方法反弧角值,但这也会导致应用程序崩溃......
为什么无法创建计数器时钟视图对象并将其引用到相同类型的 xml 布局元素,并更改其外观、使其无效等。 ?
编辑日志猫:
threadid=1: thread exiting with uncaught exception (group=0x40015560)
ERROR/AndroidRuntime(487): FATAL EXCEPTION: main
ERROR/AndroidRuntime(487): java.lang.RuntimeException: Unable to start activity ComponentInfo{training.timer/training.timer.RunTrainingWorkoutsView}: java.lang.NullPointerException
ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
ERROR/AndroidRuntime(487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
ERROR/AndroidRuntime(487): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
ERROR/AndroidRuntime(487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
ERROR/AndroidRuntime(487): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(487): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(487): at android.app.ActivityThread.main(ActivityThread.java:3683)
ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invoke(Method.java:507)
ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
ERROR/AndroidRuntime(487): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(487): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(487): at training.timer.RunTrainingWorkoutsView.onCreate(RunTrainingWorkoutsView.java:72)
ERROR/AndroidRuntime(487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
ERROR/AndroidRuntime(487): ... 11 more