0

所以我在 onPostExecute 方法期间填充 listActivity 时遇到问题。SingleData的结果列表是正确的...然后我使用自定义适配器绑定到android.R.id.list...

但它给了我错误......我真的不明白为什么......所以这是适配器的代码......:

private class RequestAdapter extends ArrayAdapter<SingleData> {

    private int _resource;
    private LayoutInflater _inflater;
    private Context _context;

    public RequestAdapter(Context context, int textViewResourceId,
            List<SingleData> objects) {
        super(context, textViewResourceId, objects);
        _resource = textViewResourceId;
        _context = context;
        _inflater = LayoutInflater.from(_context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = (ListView)_inflater.inflate(_resource,null);
        SingleData sdd = getItem(position);

        ImageView iv = (ImageView)convertView.findViewById(R.id.imagePlaceHolder_IV);
        iv.setImageBitmap(sdd.thumbIMG);

        TextView tv = (TextView)convertView.findViewById(R.id.createdAt_TV);
        tv.setText(sdd.timeStamp);

        // TODO Auto-generated method stub
        return convertView;         
    }

}

这是 onPostExecute:

private class DownloadTheImages extends AsyncTask<Void, Void, List<SingleData>> {

    private Context _ctx;

    public DownloadTheImages(Context ctx) {
        _ctx = ctx;
    }

     protected void onPostExecute(List<SingleData> result) {             

         ListView mainLV = (ListView)findViewById(android.R.id.list);
         mainLV.setAdapter(new RequestAdapter(_ctx, R.layout.single_row, result));

     }

    protected List<SingleData> doInBackground(Void... params) {
              ..... 
             ....
              }
       }

和 LOGCAT:

10-20 15:02:55.166: E/AndroidRuntime(761): FATAL EXCEPTION: main
10-20 15:02:55.166: E/AndroidRuntime(761): java.lang.NullPointerException
10-20 15:02:55.166: E/AndroidRuntime(761):  at com.mimiedesign.dingle.PictureStreamViewer$DownloadTheImages.onPostExecute(PictureStreamViewer.java:103)
10-20 15:02:55.166: E/AndroidRuntime(761):  at com.mimiedesign.dingle.PictureStreamViewer$DownloadTheImages.onPostExecute(PictureStreamViewer.java:1)
10-20 15:02:55.166: E/AndroidRuntime(761):  at android.os.AsyncTask.finish(AsyncTask.java:417)
10-20 15:02:55.166: E/AndroidRuntime(761):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-20 15:02:55.166: E/AndroidRuntime(761):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)

任何线索?...谢谢..

这些是较新的错误:

10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.ListView.fillDown(ListView.java:670)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.ListView.fillFromTop(ListView.java:727)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.ListView.layoutChildren(ListView.java:1598)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.AbsListView.onLayout(AbsListView.java:1260)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.view.View.layout(View.java:7175)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.view.View.layout(View.java:7175)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.view.View.layout(View.java:7175)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.view.View.layout(View.java:7175)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
10-20 16:32:56.615: E/AndroidRuntime(1487):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
4

2 回答 2

0

这些行导致了这个问题。

     RequestAdapter adapter; // This will be member variable
     ListView mainLV = (ListView)findViewById(android.R.id.list);
     mainLV.setAdapter(new RequestAdapter(_ctx, R.layout.single_row, result));

在 onCreate() 中移动这些行并在实例化 Adapter 之前初始化结果,然后在 onPostExecute 中简单地将值添加到“result”ArrayList 并调用 adapter.notifyDatasetChanged()。避免在 onPostExecute() 或 onProgressUpdate() 中查找视图。

于 2012-10-20T15:24:10.727 回答
0

我理解 onPostExecute 的工作方式是它接受从 doInBackground() 传入的结果对象。因此,除非您在 doinbackground 中返回该结果,然后以这种方式将其传递给您的 RequestAdapter,否则您将获得 NullPointer。你在哪里初始化你的结果成员?在 OnCreate?

 protected List<SingleData> doInBackground(Void... reuslt) {
          ..... 
         ....
       ///Do some stuff

            return result;
          }
   }


private class DownloadTheImages extends AsyncTask<Void, Void, List<SingleData>> {

private Context _ctx;

public DownloadTheImages(Context ctx) {
    _ctx = ctx;
}
于 2012-10-20T15:24:16.683 回答