1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android自定义视图添加布局文件 android – 添加自定义视图作为XML布局的视图

android自定义视图添加布局文件 android – 添加自定义视图作为XML布局的视图

时间:2018-12-17 05:58:26

相关推荐

android自定义视图添加布局文件 android  – 添加自定义视图作为XML布局的视图

方案如下:

我有一个活动RunTrainingWorkoutsView,它使用XML布局_run_workout.xml_,其中一些标签由CountDownTimer更新.工作正常…

现在,除了每秒通过onTick()CountDownTimer对象的回调方法更新的标签,我想在我的_run_workout.xml layout_中添加一个自定义表面视图,它将绘制一些每秒由同一onTick()方法更新的弧. .

我的run_workout.xml:

android:id="@+id/counter_clock_surface"

android:layout_width="300dp"

android:layout_height="240dp">

我的自定义View扩展了surfaceView

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);

}

扩展活动的主要类是使用以下代码在布局中获取对自定义surfaceView的引用:

//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;

将崩溃应用程序…

另外,从我的主要活动开始,我想调用invalidate()方法在更改counterArcAngle值后重做表面视图,但这会导致应用程序崩溃…

为什么不能创建counterClockView对象并将其引用到相同类型的xml布局元素并更改其外观,使其无效等?

编辑LogCat:

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

解决方法:

经过3天的攻击,我的头靠在墙上,谷歌搜索,stacOverflowing等我得到了它.

实际上,这是一个愚蠢的小事……

我的XML文件,我定义了包含一些常见的android视图(即textView和按钮)的布局和我的自定义视图CounterClockView:

android:id="@+id/counter_clock_surface"

android:layout_width="300dp"

android:layout_height="240dp">

我必须增加一条线!

xmlns:android="/apk/res/android" !!!

android:id="@+id/counter_clock_surface"

android:layout_width="300dp"

android:layout_height="240dp">

我不知道为什么这个命名空间行产生了如此巨大的差异,但它的效果很好!

现在,我可以在CountDownTimer()的每个onTick()上从我的主要活动更新我的自定义视图…

标签:android,android-layout,surfaceview

来源: https://codeday.me/bug/1003/1846023.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。