BaseAdapter 类不会在 Asynctask 中设置适配器 - Android

Posted

技术标签:

【中文标题】BaseAdapter 类不会在 Asynctask 中设置适配器 - Android【英文标题】:BaseAdapter class wont setAdapter inside Asynctask - Android 【发布时间】:2013-07-16 14:58:13 【问题描述】:

我有收集用户名、cmets 和数字的异步任务。它将它们放入字符串中,然后假设调用 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(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();
   

这是我的 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)

【问题讨论】:

请看我的回答。我添加了一些代码。 【参考方案1】:

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

更改 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);

【讨论】:

ctx_invitation 我也应该设置什么?因为当我尝试将它放在 CreateCommentLists mycmlist = new CreateCommentLists(cmets, usernames, numbers);声明它会导致错误【参考方案2】:

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

onPostExecute 包含(在 AsyncTask 内)

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

在你的适配器类中添加这个构造函数

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

祝你好运。

南达基肖 P

【讨论】:

【参考方案3】:

现在 ctx_invitation 永远不会被赋值。您可能需要一个构造函数来为 ctx_invitation 赋值。

【讨论】:

【参考方案4】:

四处搜索,查看this question 看起来您可能正在从适配器的 getView 方法返回一个空视图。尝试在 getView 方法中设置一些断点并找出返回空视图的位置。

另外,您将 ctx_invitation 的引用存储在 BaseAdapter 的什么位置。

在构造函数中你可能想把它传入

【讨论】:

以上是关于BaseAdapter 类不会在 Asynctask 中设置适配器 - Android的主要内容,如果未能解决你的问题,请参考以下文章

android -BaseAdapter 不会向 listView 添加新项目

为啥我的 BaseAdapter 类不增加 getView 中的位置?

如何在 Android 中获取 BaseAdapter 的上下文

简单的适配器BaseAdapter

BaseAdapter,SimpleAdapter,CursorAdapter的用法

构建一个可复用的自定义BaseAdapter