ListView不显示适配器

时间:2011-08-05 15:22:36

标签: android simpleadapter illegalstateexception

使用这段代码

            final ListView lw   =   (ListView) findViewById( R.id.book_list );
        lw.setVisibility(View.VISIBLE);

        ListView listBookView   =   (ListView) findViewById( R.id.book_list );
        ArrayList<HashMap<String, Object>> my_list  = new ArrayList<HashMap<String, Object>>();

        for ( int x=0; x< book_list.size(); x++)
        {
            String[] valori_book    =   book_list.get( x ).getObjVars();
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("book", valori_book[ 0 ]);
            map.put("descr", valori_book[ 1 ] );

            my_list.add(map);
        }
        // ...
        SimpleAdapter showMenu = new SimpleAdapter(this, my_list, R.layout.activity_book_booklistmask,
                    new String[] { "book" , "descr" }, new int[] {R.id.nome_quaderno, R.id.descrizione_quaderno});
        listBookView.setAdapter(showMenu);

java.lang.IllegalStateException:android.widget.ListView不是可由此SimpleAdapter绑定的视图

logcat没有写出我项目的错误行,所以我不知道在哪里检查:/

E/AndroidRuntime(  904): java.lang.IllegalStateException: android.widget.ListView is not a  view that can be bounds by this SimpleAdapter
E/AndroidRuntime(  904):        at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:195)
E/AndroidRuntime(  904):        at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
E/AndroidRuntime(  904):        at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
E/AndroidRuntime(  904):        at android.widget.AbsListView.obtainView(AbsListView.java:1315)
E/AndroidRuntime(  904):        at android.widget.ListView.makeAndAddView(ListView.java:1727)
E/AndroidRuntime(  904):        at android.widget.ListView.fillDown(ListView.java:652)
E/AndroidRuntime(  904):        at android.widget.ListView.fillFromTop(ListView.java:709)
E/AndroidRuntime(  904):        at android.widget.ListView.layoutChildren(ListView.java:1580)
E/AndroidRuntime(  904):        at android.widget.AbsListView.onLayout(AbsListView.java:1147)
E/AndroidRuntime(  904):        at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  904):        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
E/AndroidRuntime(  904):        at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  904):        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(  904):        at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  904):        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
E/AndroidRuntime(  904):        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
E/AndroidRuntime(  904):        at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
E/AndroidRuntime(  904):        at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  904):        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(  904):        at android.view.View.layout(View.java:7035)
E/AndroidRuntime(  904):        at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
E/AndroidRuntime(  904):        at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
E/AndroidRuntime(  904):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  904):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  904):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  904):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  904):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  904):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  904):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  904):        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

来自SimpleAdapter文档

实现Checkable的视图(例如CheckBox)。预期的绑定值是布尔值。

TextView的。预期的绑定值是一个字符串,并调用setViewText(TextView,String)。

ImageView的。预期的绑定值是资源ID或字符串,并调用setViewImage(ImageView,int)或setViewImage(ImageView,String)。

将Object作为映射对象传递似乎与SimpleAdapter不兼容。更改对象类型,或创建自定义适配器(我建议稍后)