0

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

例如创建一个类扩展列表视图, 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);
        }
    }

在这一行出现错误, View view = 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)

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

4

2 回答 2

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

试试这样。该参数在那里没有上下文,它需要一个可选的父ViewGroup根。null如果您不想使用它,请通过。

于 2013-02-09T08:38:41.157 回答
1
LayoutInflater inflater = activity.getLayoutInflater();
view = inflater.inflate(R.layout.your_layout, null);

通过扩展使用

public class yourclass extends BaseAdapter 

这可能会帮助你..

于 2013-02-09T08:43:50.630 回答