Question

I have data inside a table. when I set adapter from it, the app crashes, but if I load the info from server then everything is ok. I get NullPointerException on setting the listview's adapter. even though, I do exactly same things as when table is empty.

// check if database is empty
cursor = sh.getReadableDatabase().rawQuery(
            "SELECT * FROM " + PetopenTable.TABLE_NAME, null);
    if (cursor.getCount() < 1) {
        //cursor is empty
        tableHasData = false;
    } else {
        tableHasData = true;
    }

    if (tableHasData) {
        // we have data in database, show it, then get info
        // from server and reload the UI
        Log.d("Cursor", "PetOpenFragment; cursor is not empty");
        prepareListFromCursor(cursor);
        // initialize the list
        lview3 = (ListView) getActivity().findViewById(R.id.listView1);
        adapter = new ListViewCustomAdapter(getActivity(), itemList);
                    // here adapter is not null
        if (adapter != null) {
            Log.d("Adapter in Petopen", "is not null");
            lview3.setAdapter(adapter); // line 94
        } else {
            Log.d("ADapter in petopen", "is null");
        }

        // do not show the loading message as we can fill it instantly from database
        // now get data from server to update
        new getPetopenUsers().execute((Void) null);
    } else {
        // show loading and load from server, database is empty
        Log.d("Cursor", "PetOpenFragment; cursor is empty");
        new getPetopenUsers().execute((Void) null);
        mDialog.setMessage("Loading...");
        mDialog.setCancelable(false);
        mDialog.show();
    }
...
// inside prepareListFromCursor I initialize the itemlist
itemList = new ArrayList<Object>();
...
//when i get data from server, i initialize same as when there is data in table
lview3 = (ListView) getActivity().findViewById(R.id.listView1);
            adapter = new ListViewCustomAdapter(getActivity(), itemList);
            lview3.setAdapter(adapter);

Logcat error:

ATAL EXCEPTION: main
 java.lang.NullPointerException
    at com.petcial.petopen.fragments.petOpenFragment.onCreateView(petOpenFragment.java:94)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
    at android.os.Handler.handleCallback(Handler.java)
    at android.os.Handler.dispatchMessage(Handler.java)
    at android.os.Looper.loop(Looper.java)
    at android.app.ActivityThread.main(ActivityThread.java)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
    at dalvik.system.NativeStart.main(Native Method)

Update

Here is how i fill the itemList, i call this method after i parse the info from table

 private void AddObjectToList(String image, String name, String status,
        String distance, String location, String[] petImage,
        String[] petName) {
    // TODO Auto-generated method stub
    bean = new ItemBean();
    bean.setProfileImage(image);
    bean.setName(name);
    bean.setStatus(status);
    bean.setDistance(distance);
    bean.setLocation(location);
    bean.setPetImage(petImage);
    bean.setPetName(petName);
    itemList.add(bean);

}
Était-ce utile?

La solution

Finally I tried putting the whole thing which loads data from database inside an AsyncTask, and it worked. In case someone faced same problem here is how you have to do it:

Create an AsyncTask, which is called from onCreate or onViewCreated(as in my case, I fragment) In the doInBackground method I did not have anything, instead in the onPostExecute method, i call all the methods that processes all the data from cursor and initializes the list and adapter.

If someone has a better solution on how to make this inside an AsyncTask, please answer to this post.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top