2014-10-01 58 views
0

嘿傢伙,我想從我的數據庫中得到一些數據... 我使用SimpleCursorAdapter傳輸數據到列表視圖,但我得到一個NullPointer Eception這裏是LogCat:空指針異常,而使用SimpleCursorAdapter

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()); 
    //} 
} 

感謝您的幫助和遺憾,從德國我的壞englisch IM)

+0

Shop.java的第104行是什麼? – 2014-10-01 15:33:22

+0

'myList.setAdapter(myCursorAdapter);' – 360Productions 2014-10-01 15:34:11

+0

是的,它是在碎片......這是什麼問題? – 360Productions 2014-10-01 15:35:45

回答

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

假設列表視圖位於片段佈局中,它尚不屬於活動onCreateView()中調用的方法中活動視圖層次結構的一部分。

將您已膨脹的視圖層級作爲onCreateView()作爲根視圖調用findViewById()以在其中查找視圖。

+0

嗯,也試過這個,但它沒有工作...但現在它做...有趣..但非常感謝! – 360Productions 2014-10-01 15:37:37

0

populateListViewFromDB()的調用從onCreateView更改爲onActivityCreated,它應該工作。另外請確保R.id.listViewShop與您在onCreateView上膨脹的佈局相同。