有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用SimpleCursorAdapter时出现java空指针异常

我想从我的数据库中获取一些数据。。。 我正在使用SimpleCursorAdapter将数据传输到列表视图,但我得到了一个NullPointer异常,这里是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 安卓.app.Fragment.performCreateView(Fragment.java:1777)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.app.FragmentManagerImpl.moveToState(FragmentManager.java:1106)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.app.BackStackRecord.run(BackStackRecord.java:690)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1571)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.os.Handler.handleCallback(Handler.java:733)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.os.Handler.dispatchMessage(Handler.java:95)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.os.Looper.loop(Looper.java:157)
10-01 17:26:42.048: E/Shop(26288):  at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-01 17:26:42.048: E/Shop(26288):  at com.安卓.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());
    //}
}

共 (1) 个答案

  1. # 1 楼答案

    populateListViewFromDB()的调用从onCreateView更改为onActivityCreated,它应该可以工作。还要确保R.id.listViewShop的布局与您在onCreateView上膨胀的布局相同