-1

我有从服务器加载图像的列表适配器作为图标列表。我想我的适配器没有严重问题,但即使我已经编辑了我的代码,我仍然遇到错误。我使用 asynctask 类从服务器加载数据。这是我的适配器代码:

    public class ListAdapterImage extends BaseAdapter{

    private Activity activity;
    private Runnable runnable;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;

    public ListAdapterImage (Activity a, ArrayList<HashMap<String, String>> data) {
        activity = a;
        this.data=data;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

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

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

         View vi=v;
            if(v==null)
                vi = inflater.inflate(R.layout.list_row, null);

            TextView Pk_Merchant_ID = (TextView) vi.findViewById(R.id.tv_id);
            TextView Descriptions = (TextView) vi.findViewById(R.id.tv_groupname);
            TextView Address = (TextView) vi.findViewById(R.id.tv_merchantname);
            ImageView imgythumb = (ImageView) vi.findViewById(R.id.list_images);

            HashMap<String, String> datas = new HashMap<String, String>();
                datas = data.get(position);

            Pk_Merchant_ID.setText(datas.get("Pk_Merchant_ID"));
            Descriptions.setText(datas.get("Descriptions"));
            Address.setText(datas.get("Address"));
            imageLoader.DisplayImage(datas.get("Path_PhotoProfile"), imgythumb);
        return vi;
    }

}

在我的 logcat 中出现一些这样的错误:

11-10 22:52:02.732: E/AndroidRuntime(26274): FATAL EXCEPTION: main
11-10 22:52:02.732: E/AndroidRuntime(26274): java.lang.NullPointerException
11-10 22:52:02.732: E/AndroidRuntime(26274):    at com.example.adapter.ListAdapterImage.<init>(ListAdapterImage.java:30)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at com.example.main.ResultRestoActivity$LoadData$1.run(ResultRestoActivity.java:271)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at android.app.Activity.runOnUiThread(Activity.java:3743)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at com.example.main.ResultRestoActivity$LoadData.onPostExecute(ResultRestoActivity.java:258)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at com.example.main.ResultRestoActivity$LoadData.onPostExecute(ResultRestoActivity.java:1)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at android.os.AsyncTask.finish(AsyncTask.java:417)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at android.os.Looper.loop(Looper.java:130)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at java.lang.reflect.Method.invokeNative(Native Method)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at java.lang.reflect.Method.invoke(Method.java:507)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-10 22:52:02.732: E/AndroidRuntime(26274):    at dalvik.system.NativeStart.main(Native Method)

在我的活动中,我将适配器设置为onPostExecute

    @Override
    protected void onPostExecute(String s) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {  
                adapter = new ListAdapterImage(null, listData);
                lv.setAdapter(adapter);
            }
        });
    }

立即需要您的所有帮助和评论。感谢提前。

4

1 回答 1

0

你在这里传递 null ..

adapter = new ListAdapterImage(null, listData);

然后在这段代码中

public ListAdapterImage (Activity a, ArrayList<HashMap<String, String>> data) {
    activity = a;
    this.data=data;
    //Problem
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

经过adapter = new ListAdapterImage(yourActivityName.this, listData);

于 2012-11-10T18:25:10.170 回答