代码之家  ›  专栏  ›  技术社区  ›  Oh Danny Boy

带有标题的ListView。使用数据库数据崩溃分离的ListAdapter

  •  1
  • Oh Danny Boy  · 技术社区  · 14 年前

    我正在尝试创建一个基于多个预定义类别的带有标题的ListView。

    我正在使用 Jeff Sharkey's separating lists with headers 作为基础。类和XML是相同的,除了SimpleAdapter。区别在于我的数据来自本地的sqlite数据库。当我切换到视图之外时,代码崩溃。你知道为什么会这样吗?错误和下面的代码。

    07-15 14:58:44.950: ERROR/AndroidRuntime(582): Uncaught handler: thread main exiting due to uncaught exception
    07-15 14:58:44.970: ERROR/AndroidRuntime(582): java.lang.NullPointerException
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:149)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at com.XYZ.XYZ.SeparatedListAdapter.getView(SeparatedListAdapter.java:94)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.AbsListView.obtainView(AbsListView.java:1256)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.ListView.makeAndAddView(ListView.java:1668)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.ListView.fillDown(ListView.java:637)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.ListView.fillSpecific(ListView.java:1224)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.ListView.layoutChildren(ListView.java:1499)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.View.layout(View.java:6830)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.os.Looper.loop(Looper.java:123)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at android.app.ActivityThread.main(ActivityThread.java:4363)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-15 14:58:44.970: ERROR/AndroidRuntime(582):     at dalvik.system.NativeStart.main(Native Method)
    

    Category.java -我在Starkey的教程中找到的simpleadapter:

    package com.XYZ.ipantrylite;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;
    
    public class Category extends Activity {
    
        public final static String ITEM_TITLE = "title";
        public final static String ITEM_CAPTION = "caption";
    
        public SeparatedListAdapter adapter;
    
        private static final String TAG = "Category";
    
        public Map<String,?> createItem(String title, String caption) {
            Map<String,String> item = new HashMap<String,String>();
            item.put(ITEM_TITLE, title);
            item.put(ITEM_CAPTION, caption);
            return item;
        }
    
        @Override
    //  protected void onCreate(Bundle savedInstanceState) {
    //      // TODO Auto-generated method stub
    //      super.onCreate(savedInstanceState);
    
        public void onCreate(Bundle icicle) {  
            super.onCreate(icicle); 
    
            // create our list and custom adapter
            adapter = new SeparatedListAdapter(this);
            fillData();
    
            ListView list = new ListView(this);
            list.setTextFilterEnabled(false);
            list.setAdapter(adapter);
            this.setContentView(list);
        }
    
        private void fillData()
        {
            String[] types = new String[] { 
                    "Baby Food", 
                    "Baking Supplies", 
                    "Beverages", 
                    "Dairy Products & Eggs",
                    "Fresh Produce, Fruits & Vegetables",
                    "Canned, Dried, Frozen Goods",
                    "Grains, Cereals, Breads, Pasta",
                    "Herbs & Spices",
                    "Misc Canned Goods",
                    "Oils & Fats",
                    "Prepackaged Foods",
                    "Protein, Meat, Fish",
                    "Snacks & Sweets",
                    "Soups, Sauces, & Gravies",
                    "Toiletries"};
    
            for(int i = 0; i < types.length - 1; i++) 
            {           
                Cursor c = managedQuery(PantryItemsProviderLite.CONTENT_URI, null, "TYPE = '"+types[i]+"'", null, "name desc");
    
                startManagingCursor(c);
    
                if (c.moveToFirst()) {
                    c.setNotificationUri(getContentResolver(), PantryItemsProviderLite.CONTENT_URI); 
                    String[] from = new String[] { PantryItemsProviderLite.NAME};
                    int[] to = new int[] { R.id.textName};
    
                    SimpleCursorAdapter items = new SimpleCursorAdapter(this, R.layout.items_row_cat, c, from, to);
                    adapter.addSection(types[i], items);
                }
    
                adapter.notifyDataSetChanged();
            }
        }
    
        @Override
        protected void onPause(){
            super.onPause();
            clearData();
            Log.d("","on Pause called");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            fillData();
            Log.d("","on Resume called");
        }
    
        public void clearData(){
            adapter.headers.clear();
            adapter.sections.clear();
        }
    }
    

    崩溃时调试器中引用的代码行:

    Line 94
    if(position < size) return adapter.getView(position - 1, convertView, parent);
    
    1 回复  |  直到 12 年前
        1
  •  0
  •   Jon Adams    12 年前

    我通过使用更新后的独立数据适配器代码 http://code.google.com/p/foursquared/source/browse/main/src/com/joelapenna/foursquared/widget/SeparatedListAdapter.java 然后 onPause 我用 adapter.clear() 然后 onResume 我续杯。我会通知FillData循环中的数据更改。工作。