1

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)
4

4 回答 4

1

您是否检查View过从 getView() 方法返回的 v 是否为空?捕获Exception捕获所有异常。在我看来不是很有帮助。这里发生的事情是你没有启动Context ctx_invitation。您应该在构造函数中执行此操作。现在,由于ctx_invitation是 null,它会导致NullPointerException被 catch 块捕获。并且Viewv 保持为空。

更改 CreateCommentLists 的构造函数:

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

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

更改以下内容:

CreateCommentLists mycmlist = new CreateCommentLists(comments, usernames, numbers, DashboardActivity.this);
于 2013-07-17T21:26:46.163 回答
1

我希望这可能会有所帮助,

onPostExecute 包括(在 AsyncTask 内)

ExampleAdapter sectionedAdapter = new EfficientAdapter(ClassInfoThread.this,getBaseContext()); listView.setAdapter(sectionedAdapter);

并在您的适配器类中添加此构造函数

public ExampleAdapter(ExampleThread exampleThread,Context context) { mInflater = LayoutInflater.from(context); }

祝你好运。

南达基肖尔 P

于 2013-12-05T08:41:44.870 回答
0

现在 ctx_invitation 从未被赋值。您可能需要一个构造函数来为 ctx_invitation 赋值。

于 2013-07-17T21:22:54.140 回答
0

谷歌搜索,看着这个问题看起来你可能会从你的适配器的 getView 方法返回一个空视图。尝试在 getView 方法中设置一些断点并找出返回空视图的位置。

此外,您在哪里存储对 BaseAdapter 中 ctx_invitation 的引用。

在构造函数中,您可能希望将其传入

于 2013-07-17T21:23:13.343 回答