Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在自定义视图中获取作为引用给定的组件?_Android_Custom Component - Fatal编程技术网

Android 如何在自定义视图中获取作为引用给定的组件?

Android 如何在自定义视图中获取作为引用给定的组件?,android,custom-component,Android,Custom Component,嗨,我正在制作一个自定义视图,并在其中提供其他组件id作为参考,如下所示 <com.example.timerview.CustomView android:id="@+id/custom_view" android:layout_width="100dip" android:layout_height="100dip" mtv:associatedTextView="@+id/text_view" /> <TextView andr

嗨,我正在制作一个自定义视图,并在其中提供其他组件id作为参考,如下所示

<com.example.timerview.CustomView
    android:id="@+id/custom_view"
    android:layout_width="100dip"
    android:layout_height="100dip"
    mtv:associatedTextView="@+id/text_view"
  />
<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

但是我发现
tText
null
请告诉我哪里错了或者怎么做。请帮助我。

您的文本视图不是自定义视图的子视图,这就是为什么
findViewById
无法找到它的原因

附加自定义视图后,从根视图调用
findViewById

protected void onAttachedToWindow() {
  super.onAttachedToWindow();
  int tId = a.getResourceId(R.styleable.CustomView_associatedTextView, 0);
  TextView tText = (TextView) getRootView().findViewById(tId);
}

findviewbyd(R.id.text\u视图)有什么问题?其也返回null
protected void onAttachedToWindow() {
  super.onAttachedToWindow();
  int tId = a.getResourceId(R.styleable.CustomView_associatedTextView, 0);
  TextView tText = (TextView) getRootView().findViewById(tId);
}