带有标题的Listview。 SeparatedListAdapter使用数据库数据崩溃

时间:2010-07-15 15:12:43

标签: android listview

我正在尝试使用基于许多预定义类别的标题创建ListView。

我使用Jeff Sharkey's separating lists with headers作为基础。除了SimpleAdapter之外,类和XML是相同的。不同之处在于我的数据来自本地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 个答案:

答案 0 :(得分:0)

我使用http://code.google.com/p/foursquared/source/browse/main/src/com/joelapenna/foursquared/widget/SeparatedListAdapter.java中更新的SeperatedListAdapter代码,然后onPause我使用adapter.clear(),然后onResume我重新填充,让它工作。我通知fillData循环中的数据更改。工作