使用SimpleCursorAdapter时出现空指针异常

时间:2014-10-01 15:31:20

标签: java android sql listview simplecursoradapter

嘿伙计我想从我的数据库中获取一些数据...... 我正在使用SimpleCursorAdapter将数据传输到列表视图,但我得到的是NullPointer Eception,这里是LogCat:

10-01 17:26:42.048: E/Shop(26288): java.lang.NullPointerException
10-01 17:26:42.048: E/Shop(26288):  at       .diamond.clickers.Shop.populateListViewFromDB(Shop.java:104)
10-01 17:26:42.048: E/Shop(26288):  at com.diamond.clickers.Shop.onCreateView(Shop.java:51)
10-01 17:26:42.048: E/Shop(26288):  at android.app.Fragment.performCreateView(Fragment.java:1777)
10-01 17:26:42.048: E/Shop(26288):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
10-01 17:26:42.048: E/Shop(26288):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1106)
10-01 17:26:42.048: E/Shop(26288):  at android.app.BackStackRecord.run(BackStackRecord.java:690)
10-01 17:26:42.048: E/Shop(26288):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1571)
10-01 17:26:42.048: E/Shop(26288):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
10-01 17:26:42.048: E/Shop(26288):  at android.os.Handler.handleCallback(Handler.java:733)
10-01 17:26:42.048: E/Shop(26288):  at android.os.Handler.dispatchMessage(Handler.java:95)
10-01 17:26:42.048: E/Shop(26288):  at android.os.Looper.loop(Looper.java:157)
10-01 17:26:42.048: E/Shop(26288):  at android.app.ActivityThread.main(ActivityThread.java:5867)
10-01 17:26:42.048: E/Shop(26288):  at java.lang.reflect.Method.invokeNative(Native Method)
10-01 17:26:42.048: E/Shop(26288):  at java.lang.reflect.Method.invoke(Method.java:515)
10-01 17:26:42.048: E/Shop(26288):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-01 17:26:42.048: E/Shop(26288):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
10-01 17:26:42.048: E/Shop(26288):  at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

void populateListViewFromDB() {

    Cursor cursor = myDb.getAllRows();

    // Allow activity to manage lifetime of the cursor.
    // DEPRECATED! Runs on the UI thread, OK for small/short queries.
    getActivity().startManagingCursor(cursor);
    //if (cursor.moveToLast()) {
        //  do {
        // do what you need with the cursor here


    // Setup mapping from cursor to view fields:
    String[] fromFieldNames = new String[] 
            {DBAdapter.KEY_NAME,DBAdapter.KEY_PRICE,DBAdapter.KEY_LEVEL,DBAdapter.KEY_ART,DBAdapter.KEY_VALUETXT,DBAdapter.KEY_PIC };
    int[] toViewIDs = new int[]
            {R.id.tv_Shop_item1,     R.id.tv_Shop_Price_item1,R.id.tv_shop_Level_item1,R.id.tv_shop_use_item1,R.id.tv_shop_use_item2,R.id.IV_Shop_item1 };

    // Create adapter to may columns of the DB onto elemesnt in the UI.
    SimpleCursorAdapter myCursorAdapter = 
            new SimpleCursorAdapter(
                    getActivity().getApplicationContext(),      // Context
                    R.layout.item_shop, // Row layout template
                    cursor,                 // cursor (set of DB records to map)
                    fromFieldNames,         // DB Column names
                    toViewIDs               // View IDs to put information in
                    );

    // Set the adapter for the list view
    ListView myList = (ListView)getActivity().findViewById(R.id.listViewShop);
    myList.setAdapter(myCursorAdapter);
     //  } while (cursor.moveToPrevious());
    //}
}

感谢帮助和抱歉我来自德国的糟糕英语;)

2 个答案:

答案 0 :(得分:1)

getActivity().findViewById(R.id.listViewShop)

假设listview位于片段布局中,它还不是活动onCreateView()调用的方法中活动视图层次结构的一部分。

将您在onCreateView()中充气的视图层次结构作为您调用findViewById()的根视图传递,以查找其中的视图。

答案 1 :(得分:0)

populateListViewFromDB()的来电从onCreateView更改为onActivityCreated,这应该有效。另外,请确保R.id.listViewShop与您在onCreateView上充气的布局相同。