Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

string - BaseAdapter class wont setAdapter inside Asynctask - Android

I have asynctask that gathers usernames, comments, and numbers. It places them into strings and is then suppose to call a BaseAdapter class, create an adapter, and set the adapter to the class. But my code doesn't work, it crashes the app, here is my code

   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(String[] comments, String[] usernames, String[] numbers)
            {
                super();

                listComments = comments;
                listNumbers = usernames;
                listUsernames = numbers;
            }

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

                // 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;
            }

          }




        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);
                            }

                            CreateCommentLists mycmlist = new CreateCommentLists(comments, usernames, numbers);
                            lstComments = (ListView)findViewById(android.R.id.list);

                            lstComments.setAdapter(mycmlist);








                            }//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();
   }

Here is my logCat

    07-17 17:07:13.436: E/AndroidRuntime(23880): FATAL EXCEPTION: main
07-17 17:07:13.436: E/AndroidRuntime(23880): java.lang.NullPointerException
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.ListView.measureScrapChild(ListView.java:1163)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1294)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.ListView.onMeasure(ListView.java:1155)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at com.navdrawer.SimpleSideDrawer.onMeasure(SimpleSideDrawer.java:443)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2162)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.View.measure(View.java:13011)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1091)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.os.Looper.loop(Looper.java:154)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at android.app.ActivityThread.main(ActivityThread.java:4945)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 17:07:13.436: E/AndroidRuntime(23880):    at dalvik.system.NativeStart.main(Native Method)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have you checked if the View v that you return from your getView() method is null? Catching Exception catches all exceptions. Not really helpful in my opinion. What's happening here is that you have not initiated Context ctx_invitation. You should do this in the constructor. Now, since ctx_invitation is null, it causes a NullPointerException which is caught by the catch block. and View v remains null.

Change the constructor for CreateCommentLists:

public CreateCommentLists(String[] comments, String[] usernames, String[] numbers, DashboardActivity context)
        {
            super();

            listComments = comments;
            listNumbers = usernames;
            listUsernames = numbers;
            ctx_invitation = context;
        }

Change the following as well:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...