0

我正在尝试在扩展 ListView 的 android 中创建自定义 ViewGroup。

ViewGroup 类如下所示。

public class SideBarView extends ListView implements View.OnClickListener, View.OnLongClickListener {
SideConnectionAdapter sideConnectionAdapter;

public SideBarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    onCreateView();
}

public SideBarView(Context context, AttributeSet attrs) {
    super(context, attrs);
    onCreateView();
}

public SideBarView(Context context) {
    super(context);
    onCreateView();

}

private void onCreateView() {
    inflate(getContext(), R.layout.side_bar, this);

}

public void setCursorForList(Cursor cursor) {

}

@Override
public void onClick(View v) {


}

@Override
public boolean onLongClick(View v) {
    return false;
}

}

side_bar.xml 布局文件是

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/side_background">
</ListView>

我在另一个活动布局 xml 文件中使用此自定义 ViewGroup,如下所示

<RelativeLayout         xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/scrollView">


<com.zis.view_group.SideBarView
    android:id="@+id/side_bar"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"

    android:layout_alignParentTop="true"
    android:background="@drawable/side_background"/></RelativeLayout>

最后,我使用布局进行活动。对应的代码如下所示。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_message_chatroom);


    SideBarView sideBarView = (SideBarView) findViewById(R.id.side_bar);
    sideBarView.setCursorForList(getCursor());

}

但是,使用这些代码,我得到了 android.view.InflateException: Binary XML file error。

编辑:完整日志

2315-2315/com.zis.iti E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zis.iti/com.zis.iti.ChatRoomActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.zis.view_group.SideBarView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2072)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2097)
        at android.app.ActivityThread.access$600(ActivityThread.java:136)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4787)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.zis.view_group.SideBarView
        at android.view.LayoutInflater.createView(LayoutInflater.java:613)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
        at com.zis.iti.ChatRoomActivity.onCreate(ChatRoomActivity.java:54)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2097)
        at android.app.ActivityThread.access$600(ActivityThread.java:136)

            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4787)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        at android.view.LayoutInflater.createView(LayoutInflater.java:587)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
        at com.zis.iti.ChatRoomActivity.onCreate(ChatRoomActivity.java:54)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2097)
        at android.app.ActivityThread.access$600(ActivityThread.java:136)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4787)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
        at android.widget.AdapterView.addView(AdapterView.java:477)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:497)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at android.view.View.inflate(View.java:16119)
        at com.zis.view_group.SideBarView.onCreateView(SideBarView.java:36)
        at com.zis.view_group.SideBarView.<init>(SideBarView.java:26)
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        at android.view.LayoutInflater.createView(LayoutInflater.java:587)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
        at com.zis.iti.ChatRoomActivity.onCreate(ChatRoomActivity.java:54)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2097)
        at android.app.ActivityThread.access$600(ActivityThread.java:136)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4787)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
        at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0