0

我有一个异步任务,它收集用户名、评论和数字并将它们放入字符串中。然后假设这些字符串用于 BaseAdapter,但是当 BaseAdapter 被调用时,它表示字符串变量为空。这是我当前的代码,

    public class DashboardActivity extends ListActivity {
     String comments[];
     String usernames[];
     String numbers[];
     ListView lstComments;


    class CreateCommentLists extends BaseAdapter{
            Context ctx_invitation;
            String[] listComments;
            String[] listNumbers;
            String[] listUsernames;


            public CreateCommentLists(Context ctx_invitation, String[] comments, String[] usernames, String[] numbers)
            {
                super();
                this.ctx_invitation = ctx_invitation;
                listComments = comments;
                listNumbers = usernames;
                listUsernames = numbers;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return listComments.length;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return listComments[position];
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View v = null;
                try
                {
                    String inflater = Context.LAYOUT_INFLATER_SERVICE;
                    LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
                    v = li.inflate(R.layout.list_item, null);

                    TextView commentView = (TextView)v.findViewById(R.id.listComment);
                    TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
                    TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);


                    commentView.setText(listComments[position]);
                    NumbersView.setText(listNumbers[position]);
                    usernamesView.setText(listUsernames[position]);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                return v;
            }

          }

         final CreateCommentLists mycmlist = new CreateCommentLists(this, comments, usernames, numbers);




        class loadComments extends AsyncTask<JSONObject, String, JSONObject> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();


            } 

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);

            } 

            protected JSONObject doInBackground(JSONObject... params) {


                JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);


                    return json2;



            }

            @Override
            protected void onPostExecute(JSONObject json2) {
                try {  
                    if (json2.getString(KEY_SUCCESS) != null) { 
                        registerErrorMsg.setText("");
                        String res2 = json2.getString(KEY_SUCCESS);
                        if(Integer.parseInt(res2) == 1){ 



                            /*JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
                            String comments[] = new String[commentArray.length()];
                            for ( int i=0; i<commentArray.length(); i++ ) {
                                comments[i] = commentArray.getString(i);
                            }
                            JSONArray numbersArray = json2.getJSONArray(KEY_NUMBER);
                            String numbers[] = new String[numbersArray.length()];
                            for ( int i=0; i<numbersArray.length(); i++ ) {
                                numbers[i] = numbersArray.getString(i);
                            }
                            JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
                            String usernames[] = new String[usernameArray.length()];
                            for ( int i=0; i<usernameArray.length(); i++ ) {
                                usernames[i] = usernameArray.getString(i);
                            }*/








                            }//end if key is == 1
                        else{
                            // Error in registration
                            registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
                        }//end else
                    }//end if
                } //end try

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


        new loadComments().execute();

    }

这是我的 LogCat,

    07-16 22:00:05.866: D/AndroidRuntime(10072): Shutting down VM
07-16 22:00:05.866: W/dalvikvm(10072): threadid=1: thread exiting with uncaught exception (group=0x40ac9228)
07-16 22:00:05.896: E/AndroidRuntime(10072): FATAL EXCEPTION: main
07-16 22:00:05.896: E/AndroidRuntime(10072): java.lang.NullPointerException
07-16 22:00:05.896: E/AndroidRuntime(10072):    at com.example.DashboardActivity$1CreateCommentLists.getCount(DashboardActivity.java:389)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at android.widget.ListView.setAdapter(ListView.java:460)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at com.example.DashboardActivity$1loadComments.onPostExecute(DashboardActivity.java:539)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at com.example.DashboardActivity$1loadComments.onPostExecute(DashboardActivity.java:1)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at android.os.AsyncTask.finish(AsyncTask.java:602)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at android.os.Looper.loop(Looper.java:154)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at android.app.ActivityThread.main(ActivityThread.java:4945)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at java.lang.reflect.Method.invokeNative(Native Method)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at java.lang.reflect.Method.invoke(Method.java:511)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 22:00:05.896: E/AndroidRuntime(10072):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

AsyncTask正在从启动它的 Activity 异步执行。因此,当您正在创建活动并因此呈现列表时,您AsyncTask可能仍在加载评论。

你应该用一个空的适配器来初始化你的列表,或者就像已经建议的那样,用空的集合来初始化你的局部变量。

然后您可以通过更改适配器的onPostExecute()方法将数据添加到列表中。通过这种方式,您将确保仅在数据可用时更新。AsyncTasksetAdapter()ListView

于 2013-07-17T02:14:06.920 回答
0
Check in constructor of CreateCommentLists, if the comments you pass is null or not. Where are you calling the constructor and are you initialising the data before data in your AsyncTask?
You seem to pass null comments, thats why u get null pointer exception in getCount method of your adapter Change to this   

@覆盖

      public int getCount() {
if(null == listComments)
{
return 0;
}

                // TODO Auto-generated method stub
                return listComments.length;
            }
于 2013-07-17T02:08:00.017 回答
0

您没有初始化“评论”和/或给它任何数据,因此它由于 NullPointerError 而崩溃。创建适配器时,构造函数中的所有参数都必须有效,因为适配器将调用 getCount() 来确定要显示多少列表项。

如果您从 Array 切换到类似ArrayList的东西,您可以创建一个空列表并随时添加到其中。

public class DashboardActivity extends ListActivity {
    List<String> comments = new ArrayList<String>();
    List<String> usernames = new ArrayList<String>();
    List<String> numbers = new ArrayList<String>();
    ListView lstComments;
    ...
于 2013-07-17T02:09:18.230 回答