我正在创建一个自定义复合布局,它由一个可点击的水平 LinearLayout 组成,其中包含一个 ImageView 和两个 TextView。最终,我希望能够使用单个字段引用整个内容,并根据用户 Activity 对我的 Activity 进行添加、删除和编辑。我正在使用我在构造函数中膨胀的 XML 资源。但是我的手机认为我正在创建一个 LinearLayout,当我尝试实现自定义类时出现以下错误:
D/ConnectionButton﹕ Ready to inflate
D/ConnectionButton﹕ constructor 2
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417be898)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampleapp.app/com.exampleapp.app.ManageFriends}: android.view.InflateException: Binary XML file line #3: Error inflating class com.exampleapp.utils.ConnectionButton
设置NameText时的错误。如果我离开那条线,它会运行,但我后来的日志告诉我 android 认为它是 LinearLayout,而不是 ConnectionButton
所以这是我的定义类 ConnectionButton:
public class ConnectionButton extends LinearLayout {
ImageView IconView;
TextView NameView;
TextView StatusView;
public ConnectionButton(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ConnectionButton","Ready to inflate");
addView(inflater.inflate(R.layout.connection_row, null));
Log.d("ConnectionButton","Inflated");
IconView = (ImageView) findViewById(R.id.icon);
NameView = (TextView) findViewById(R.id.name);
StatusView = (TextView) findViewById(R.id.status);
}
public ConnectionButton (Context context, AttributeSet attrs) {
super(context, attrs);
Log.d("ConnectionButton","constructor 2");
/* LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ConnectionButton","Ready to inflate");
addView(inflater.inflate(R.layout.connection_row, null));
Log.d("ConnectionButton","Inflated");*/
IconView = (ImageView) this.findViewById(R.id.icon);
NameView = (TextView) this.findViewById(R.id.name);
StatusView = (TextView) this.findViewById(R.id.status);
NameView.setText("From Constructor 2");
}
}
这是我的 XML 资源:
<com.exampleapp.utils.ConnectionButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:clickable="true"
style="@android:style/Widget.Button">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:adjustViewBounds="true"
android:paddingRight="5dp"
android:id="@+id/icon"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_weight="2"
android:id="@+id/name"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_weight="1"
android:id="@+id/status"/>
</com.exampleapp.utils.ConnectionButton>
这里是我创建它的地方:(注意 ConnectionList 是应该包含这些对象的现有垂直 LinearLayout)
LinearLayout box = (LinearLayout)findViewById(R.id.ConnectionList);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ConnectionButton","Ready to inflate");
View CBv = inflater.inflate(R.layout.connection_row, box);
Log.d("CBv class",CBv.getClass().toString());
最佳答案
您错误地假设 inflater.inflate 会返回新 View 。根据它返回的文档:
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.
因此,在您的情况下,CBv 是一个 LinearLayout,因为这就是父级。要访问您的 View ,只需使用 findViewById。
关于java - Android:扩展 LinearLayout 的自定义复合类认为它是 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643499/