简体   繁体   中英

Simple Adapter - NullPointerException

atm i write a little program, which communicates with a MYSQL DB on my webserver. I use json to send a query and want to display the answer in a them in a listview.

I store the Data from json in a Arraylist> and write them later in a ListAdapter.

It works fine, but every time on the first start of the App, i get a NullPointerException and the App exits. If i start the app again, everything works good.

The Part of my Code:

public void updateJSONdata() {



    mCommentList = new ArrayList<HashMap<String, String>>();


    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

   try {

        mComments = json.getJSONArray(TAG_POSTS);

        for (int i = 0; i < mComments.length(); i++) {
            JSONObject c = mComments.getJSONObject(i);


            String id = c.getString(TAG_POST_ID);
            String username = c.getString(TAG_USERNAME);
            String title = c.getString(TAG_TITLE);
            String content = c.getString(TAG_MESSAGE);
            String datum = c.getString(TAG_DATUM);
            String aktusr = c.getString(TAG_AKTUSR);
            String maxusr = c.getString(TAG_MAXUSR);
            String gender = c.getString(TAG_GENDER);
            String activity = c.getString(TAG_ACTIVITY);
            String category = c.getString(TAG_CATEGORY);

            String cat = c.getString(TAG_CAT_ID);

            int catid = Integer.parseInt(cat);


            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();


            map.put(TAG_POST_ID, id);
            map.put(TAG_USERNAME, username);
            map.put(TAG_TITLE, title);
            map.put(TAG_MESSAGE, content);
            map.put(TAG_DATUM, datum);
            map.put(TAG_AKTUSR, aktusr); 
            map.put(TAG_MAXUSR, maxusr); 
            map.put(TAG_GENDER, gender);
            map.put(TAG_ACTIVITY, activity);
            map.put(TAG_CATEGORY, category);


            //map.put(TAG_CAT, category[catid-1]);
            map.put(TAG_PIC_ID, Integer.toString(PIC[catid-1]));

            mCommentList.add(map);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    //String[] from = {TAG_CAT, TAG_POST_ID, TAG_TITLE, TAG_MESSAGE, TAG_USERNAME};
}


private void updateList() {

    ListAdapter adapter = new SimpleAdapter(this, mCommentList,
            R.layout.single_comment, new String[] {TAG_PIC_ID,TAG_CATEGORY, TAG_ACTIVITY, TAG_DATUM, TAG_AKTUSR, TAG_MAXUSR, TAG_GENDER, /*TAG_POST_ID,*/ TAG_TITLE, TAG_MESSAGE,
                    TAG_USERNAME }, new int[] { R.id.imgrow, R.id.category, R.id.activity/*R.id.id*/ , R.id.datum, R.id.aktusr, R.id.maxusr, R.id.gender, /*R.id.category,*/ R.id.title, R.id.message,
                    R.id.username });


    setListAdapter(adapter);


    ListView lv = getListView();    
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            int intid = (int)id;
            Toast.makeText(getApplicationContext(), mCommentList.get(intid).get(TAG_POST_ID).toString(), Toast.LENGTH_SHORT).show();



        }
    });
}

public class LoadComments extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ReadComments.this);
        pDialog.setMessage("Loading Comments...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... arg0) {
        //we will develop this method in version 2
        updateJSONdata();
        return null;

    }


    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
      //we will develop this method in version 2
        updateList();
    }
}

09-27 15:14:17.593: E/AndroidRuntime(7876): FATAL EXCEPTION: main 09-27 15:14:17.593: E/AndroidRuntime(7876): Process: com.example.login_remote, PID: 7876 09-27 15:14:17.593: E/AndroidRuntime(7876): java.lang.NullPointerException 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.widget.ListView.setAdapter(ListView.java:480) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.app.ListActivity.setListAdapter(ListActivity.java:265) 09-27 15:14:17.593: E/AndroidRuntime(7876): at com.example.login_remote.ReadComments.updateList(ReadComments.java:270) 09-27 15:14:17.593: E/AndroidRuntime(7876): at com.example.login_remote.ReadComments.access$2(ReadComments.java:256) 09-27 15:14:17.593: E/AndroidRuntime(7876): at com.example.login_remote.ReadComments$LoadCategorys.onPostExecute(ReadComments.java:413) 09-27 15:14:17.593: E/AndroidRuntime(7876): at com.example.login_remote.ReadCom ments$LoadCategorys.onPostExecute(ReadComments.java:1) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.os.AsyncTask.finish(AsyncTask.java:632) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.os.AsyncTask.access$600(AsyncTask.java:177) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.os.Handler.dispatchMessage(Handler.java:102) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.os.Looper.loop(Looper.java:212) 09-27 15:14:17.593: E/AndroidRuntime(7876): at android.app.ActivityThread.main(ActivityThread.java:5151) 09-27 15:14:17.593: E/AndroidRuntime(7876): at java.lang.reflect.Method.invokeNative(Native Method) 09-27 15:14:17.593: E/AndroidRuntime(7876): at java.lang.reflect.Method.invoke(Method.java:515) 09-27 15:14:17.593: E/AndroidRuntime(7876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-27 15:14:17.593 : E/AndroidRuntime(7876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 09-27 15:14:17.593: E/AndroidRuntime(7876): at dalvik.system.NativeStart.main(Native Method)

so it looks like, the problem is the line

09-27 15:14:17.593: E/AndroidRuntime(7876): at com.example.login_remote.ReadComments.updateList(ReadComments.java:270)

setListAdapter(adapter);

Can anyone of you see the mistake? I rly dont know whats wrong here, and why the Error shows up only on the first start.

thx, Greetz AllesFAM

what your doing is calling your AsyncTask which does run the Task aside of your UI so you display the content before even getting your comments from your HTTP Request to solve that you have a lot of ways

First : you can fill the mComments Array with temporary data manually like this

mCommentList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.add("","");
mCommentList.add(map);

or Second way : you can wait for your AsyncTask result before displaying it in your UI to do this I need you code for calling the LoadComments but you can do it simply like this in your call

Boolean finished  = LoadComments.execute().get();

I call the LoadComments() in the OnResume() funktion

protected void onResume() { // TODO Auto-generated method stub super.onResume(); //loading the comments via AsyncTask new LoadComments().execute(); }

But where i have to check the finished? Cause i need it between UpdateJsonData and Update list or?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM