0

I would like to get some data from my database... I'm using the SimpleCursorAdapter to transmit the data to a list view but I get a NullPointer Exception here is the 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)

And here is my code:

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());
    //}
}
Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55

2 Answers2

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

Assuming the listview is in the fragment layout, it isn't yet a part of the activity view hierarchy in a method called from activity onCreateView().

Pass the view hierarchy you've inflated in onCreateView() as the root view you call findViewById() on to find views there.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Change the call of populateListViewFromDB() from onCreateView to onActivityCreated and it should work. Also make sure R.id.listViewShop is in the same layout as the one that you're inflating on onCreateView.

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82