简单适配器 - NullPointerException

时间:2014-09-27 13:34:35

标签: java android json nullpointerexception

atm我写了一个小程序,它与我的网络服务器上的MYSQL数据库进行通信。 我使用json发送查询,并希望在列表视图中显示答案。

我将来自json的数据存储在Arraylist中>稍后在ListAdapter中写下它们。

它工作正常,但每次在应用程序的第一次启动时,我得到一个NullPointerException并且应用程序退出。 如果我再次启动应用程序,一切正常。

我的代码的一部分:

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):致命异常:主要   09-27 15:14:17.593:E / AndroidRuntime(7876):进程: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):在android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)   09-27 15:14:17.593:E / AndroidRuntime(7876):在android.widget.ListView.setAdapter(ListView.java:480)   09-27 15:14:17.593:E / AndroidRuntime(7876):在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.ReadComments $ LoadCategorys.onPostExecute(ReadComments.java:1)   09-27 15:14:17.593:E / AndroidRuntime(7876):在android.os.AsyncTask.finish(AsyncTask.java:632)   09-27 15:14:17.593:E / AndroidRuntime(7876):在android.os.AsyncTask.access $ 600(AsyncTask.java:177)   09-27 15:14:17.593:E / AndroidRuntime(7876):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)   09-27 15:14:17.593:E / AndroidRuntime(7876):在android.os.Handler.dispatchMessage(Handler.java:102)   09-27 15:14:17.593:E / AndroidRuntime(7876):在android.os.Looper.loop(Looper.java:212)   09-27 15:14:17.593:E / AndroidRuntime(7876):在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)

所以看起来,问题就在于行

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

setListAdapter(适配器);

你们中有人能看到错误吗? 我不知道这里有什么问题,以及为什么错误只出现在第一次启动时。

THX, Greetz AllesFAM

2 个答案:

答案 0 :(得分:0)

你正在做的是调用你的AsyncTask,它会在你的UI之外运行任务,所以你甚至可以在从HTTP请求中获取你的评论之前显示内容 解决你有很多方法

首先:您可以手动填充mComments数组,如下所示

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

或第二种方式:你可以等待你的AsyncTask结果,然后再在你的UI中显示它来执行此操作我需要你的代码来调用LoadComments但你可以在调用中这样做就好了

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

答案 1 :(得分:0)

我在OnResume()funktion中调用LoadComments()

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

但我必须检查完成的?因为我在UpdateJsonData和Update列表之间需要它或者? -