Android类扩展了listview错误

时间:2013-02-09 08:30:25

标签: android listview android-listview

我正在使用一项活动,在该活动中我添加了视图。

例如创建一个类扩展列表视图, 的 mycode的:

    public class ListViewExample extends ListView{
        Context context;
        ListView list;

        public ListViewExample(Context context,String[] ListItems) {
            super(context, null);
            // TODO Auto-generated constructor stub
            this.context=context;
            this.ListItems=ListItems;


            LayoutInflater layoutInflater = 
                    (LayoutInflater)context.getSystemService   
                    (Context.LAYOUT_INFLATER_SERVICE);
            View view = layoutInflater.inflate(R.layout.list, this);
        }
    }

在此行上收到错误, 查看视图= layoutInflater.inflate(R.layout.list,this);

logcat中的

错误:

02-09 14:03:09.520: E/AndroidRuntime(11180): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.f1live.homepage/com.f1live.homepage.HomePage}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.os.Looper.loop(Looper.java:123)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at java.lang.reflect.Method.invoke(Method.java:521)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at dalvik.system.NativeStart.main(Native Method)
02-09 14:03:09.520: E/AndroidRuntime(11180): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.widget.AdapterView.addView(AdapterView.java:461)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at com.f1live.homepage.NewsListView.<init>(NewsListView.java:20)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at com.f1live.homepage.HomePage.onCreate(HomePage.java:25)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-09 14:03:09.520: E/AndroidRuntime(11180):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我很困惑为什么我收到这个错误。告诉我这样做的好方法。

2 个答案:

答案 0 :(得分:1)

View view = layoutInflater.inflate(R.layout.list, null);

尝试这样。该参数不占用上下文,它需要一个可选的父ViewGroup根。如果您不想使用它,请传递null

答案 1 :(得分:1)

LayoutInflater inflater = activity.getLayoutInflater();
view = inflater.inflate(R.layout.your_layout, null);

通过扩展

使用
public class yourclass extends BaseAdapter 

这可能对你有帮助..