添加自定义视图作为XML布局的看法 [英] Add custom view as a view of XML layout

查看:163
本文介绍了添加自定义视图作为XML布局的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该场景如下:

我有一个使用XML布局_run_workout.xml_一些标签由 CountDownTimer 的更新活动的 RunTrainingWorkoutsView 的。做工精细...

现在,除了由的 onTick()每秒更新标签的CountDownTimer对象的回调方法我想定制表面观添加到我的_run_workout.xml layout_将绘制由更新了一些弧线同样的 onTick()的方法,每一秒......

我run_workout.xml:

 < training.timer.CounterClockView
    机器人:ID =@ + ID / counter_clock_surface
    机器人:layout_width =300dp
    机器人:layout_height =240dp>
< /training.timer.CounterClockView>

我的自定义视图扩展的 surfaceView

 公共类CounterClockView延伸SurfaceView {涂料粉刷=新的油漆();
涂料paint2 =新的油漆();最后RectF RECT =新RectF();
最后RectF RECT2 =新RectF();INT counterArcAngle = 15;
//构造
公共CounterClockView(上下文的背景下,AttributeSet中的AttributeSet){
    超级(上下文);    //设置一些涂料性能...    this.setBackgroundColor(Color.TRANSPARENT);}@覆盖
公共无效的onDraw(帆布油画){    rect.set(50,50,150,150);
    rect2.set(50,50,150,150);    this.layout(0,0,200,200);    canvas.drawArc(RECT,-90,360,假的,油漆);
    canvas.drawArc(RECT2,-90,counterArcAngle,假的,paint2);}

我的延伸活动越来越引用到自定义主类的 surfaceView 的符合下列code中的布局:

  // counterClockView声明的onCreate()作为CounterClockView counterClockView之外;//后面的onCreate(){....
counterClockView =(CounterClockView)findViewById(R.id.counter_clock_surface);

问题是改变

customView对象的成员变量(counterClockView)的值

  counterClockView.counterArcAngle = 10;

会崩溃的应用程序...

另外,从我的主要活动,我想呼吁的无效()的方法改变后重新进行表面视图的 counterArcAngle 的价值,但是这会导致应用程序崩溃过...

为什么不能创建的 counterClockView 的对象,并参考同类型的XML布局元素和变化及其appereance,它失效等?

编辑LogCat中的:

 主题ID = 1:螺纹未捕获的异常退出(组= 0x40015560)ERROR / AndroidRuntime(487):致命异常:主要ERROR / AndroidRuntime(487):了java.lang.RuntimeException:无法启动活动ComponentInfo {training.timer / training.timer.RunTrainingWorkoutsView}:显示java.lang.NullPointerExceptionERROR / AndroidRuntime(487):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
ERROR / AndroidRuntime(487):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
ERROR / AndroidRuntime(487):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
ERROR / AndroidRuntime(487):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
ERROR / AndroidRuntime(487):在android.os.Handler.dispatchMessage(Handler.java:99)
ERROR / AndroidRuntime(487):在android.os.Looper.loop(Looper.java:123)ERROR / AndroidRuntime(487):在android.app.ActivityThread.main(ActivityThread.java:3683)
ERROR / AndroidRuntime(487):在java.lang.reflect.Method.invokeNative(本机方法)
ERROR / AndroidRuntime(487):在java.lang.reflect.Method.invoke(Method.java:507)
ERROR / AndroidRuntime(487):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
ERROR / AndroidRuntime(487):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
ERROR / AndroidRuntime(487):在dalvik.system.NativeStart.main(本机方法)
ERROR / AndroidRuntime(487):致:显示java.lang.NullPointerException
ERROR / AndroidRuntime(487):在training.timer.RunTrainingWorkoutsView.onCreate(RunTrainingWorkoutsView.java:72)
ERROR / AndroidRuntime(487):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR / AndroidRuntime(487):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
ERROR / AndroidRuntime(487):11 ...更多


解决方案

我撞后,我的头靠在墙上的3天,通过谷歌搜索得到它,stacOverflowing等。

其实,这是这个愚蠢的小东西...

在这里我定义包含一些常见的Andr​​oid视图(TextView的和按钮即)和我的自定义视图的布局我的XML文件中的 CounterClockView 的我有:

 < training.timer.CounterClockView
机器人:ID =@ + ID / counter_clock_surface
机器人:layout_width =300dp
机器人:layout_height =240dp>

我在那里已经增加了一个行!

 < training.timer.CounterClockView
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android!
    机器人:ID =@ + ID / counter_clock_surface
    机器人:layout_width =300dp
    机器人:layout_height =240dp>
< /training.timer.CounterClockView>

我不知道为什么这个命名空间线做如此巨大的差异,但它的伟大工程!

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

以下的答案是非常有益的:
<一href=\"http://stackoverflow.com/questions/1691569/findviewbyid-returns-null-for-custom-component-in-layout-xml-not-for-other-co\">findViewById()返回null布局XML,而不是其他组件

自定义组件

The scenario is the following:

I have an activity RunTrainingWorkoutsView that uses XML layout _run_workout.xml_ with some labels that are updated by CountDownTimer. Works fine...

Now, apart from labels that are updated every sec by onTick() callback method of CountDownTimer object I want to add a custom surface view to my _run_workout.xml layout_ that would draw some arcs updated by the same onTick() method every second...

my run_workout.xml:

<training.timer.CounterClockView 
    android:id="@+id/counter_clock_surface"
    android:layout_width="300dp" 
    android:layout_height="240dp">
</training.timer.CounterClockView>

My custom View extends 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);

}

My main class that extends activity is getting the reference to a custom surfaceView in the layout with the following code:

//counterClockView  is declared outside of onCreate() as CounterClockView counterClockView;  

//later in onCreate(){....
counterClockView  = (CounterClockView) findViewById(R.id.counter_clock_surface);

The problem is changing the value of member variables of customView object (counterClockView)

counterClockView.counterArcAngle = 10;

will crash the app...

Also, from my main activity I would like to call invalidate() method to redo the surface view after changing the counterArcAngle value, but this causes app to crash too...

Why can't create counterClockView object and reference it to a xml layout element of the same type and the change its appereance, invalidate it etc. ?

EDIT 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

解决方案

I got it after 3 days of banging my head against the wall and by googling, stacOverflowing etc.

Actually, it was this stupid little thing...

My XML file where I defined the layout containing some usual android views (textView and buttons namely) and my custom view CounterClockView I had:

<training.timer.CounterClockView 
android:id="@+id/counter_clock_surface"
android:layout_width="300dp" 
android:layout_height="240dp">

where I had to have added one more line!

<training.timer.CounterClockView 
    xmlns:android="http://schemas.android.com/apk/res/android"   !!!
    android:id="@+id/counter_clock_surface"
    android:layout_width="300dp" 
    android:layout_height="240dp">
</training.timer.CounterClockView>

I have no idea why this namespace line made such a huge difference, but it works great!

Now, I can update my custom view from my main activity on every onTick() of CountDownTimer()...

The following answer was very helpful: findViewById() returns null for custom component in layout XML, not for other components

这篇关于添加自定义视图作为XML布局的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆