0

我有一个由自定义适配器(延迟加载)填充的列表片段,它应该显示一些文本和图像。所以我构建了一个列表片段和一个自定义适配器来实现这一点。下面的代码但它抛出了显示的错误(Log cat )

ListFragment.java

@Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);

   new loadListView().execute();


 }
 protected DashBoardActivity fragmentActivity;    
 public void onAttach (DashBoardActivity activity){
     fragmentActivity = (DashBoardActivity) activity;
 }
 public class loadListView extends AsyncTask<Integer, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Integer... args) {
              motellist = new ArrayList<HashMap<String, String>>();
              JSONObject json = JSONfunctions.getJSONfromURL(URL);
              try{

                JSONArray  mot = json.getJSONArray("MOTEL");
                    for(int i=0;i<mot.length();i++){                        
                        HashMap<String, String> map = new HashMap<String, String>();        
                        JSONObject e = mot.getJSONObject(i);
                        map.put(KEY_ID, e.getString(KEY_ID));
                        map.put(KEY_NAME, e.getString(KEY_NAME));
                        map.put(KEY_PRICE, e.getString(KEY_PRICE));
                        map.put(KEY_ROOMS, e.getString(KEY_ROOMS));
                        map.put(KEY_REF, e.getString(KEY_REF));
                        map.put(KEY_PHONE, e.getString(KEY_PHONE));
                        map.put(KEY_IMAGE_URL, e.getString(KEY_IMAGE_URL));
                        motellist.add(map);
                    }       
              }catch(JSONException e)      {
                 Log.e("log_tag", "Error kupitisha data "+e.toString());
              }
                return null;
            }   


        @Override
        protected void onPostExecute(String args) {

              //list = getListView();
              adapter=new LazyAdapter (fragmentActivity, motellist);
              list.setAdapter(adapter);
            /**ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);*/

         }
    }

LazyAdapter.java

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


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


public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return data.get(position);//nilifix hapa ju am using a custom
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView NAME = (TextView)vi.findViewById(R.id.name); 
    TextView PRICE = (TextView)vi.findViewById(R.id.board); 
    TextView REF= (TextView)vi.findViewById(R.id.ref); 
    TextView ROOMS = (TextView)vi.findViewById(R.id.units); 
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); 

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

    // Setting all values in listview
    NAME.setText(hash.get(ListFragment.KEY_NAME));
    PRICE.setText("PRICE: "+hash.get(ListFragment.KEY_PRICE));
    REF.setText("REF:"+hash.get(ListFragment.KEY_REF));
    ROOMS.setText("ROOMS: "+hash.get(ListFragment.KEY_ROOMS));
    imageLoader.DisplayImage(hash.get(ListFragment.KEY_IMAGE_URL), thumb_image);
    return vi;
}

日志猫:

0-10 17:42:30.765: E/AndroidRuntime(17557): FATAL EXCEPTION: main
10-10 17:42:30.765: E/AndroidRuntime(17557): java.lang.NullPointerException
10-10 17:42:30.765: E/AndroidRuntime(17557):    at com.symetry.myitprovider.fragment.ListFragment$loadListView.onPostExecute(ListFragment.java:92)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at com.symetry.myitprovider.fragment.ListFragment$loadListView.onPostExecute(ListFragment.java:1)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at android.os.AsyncTask.finish(AsyncTask.java:590)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at android.os.AsyncTask.access$600(AsyncTask.java:149)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at android.os.Looper.loop(Looper.java:132)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at android.app.ActivityThread.main(ActivityThread.java:4123)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at java.lang.reflect.Method.invoke(Method.java:491)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
10-10 17:42:30.765: E/AndroidRuntime(17557):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

使用getActivity()代替fragmentActivityActivity可能在nullonAttach)。还要确保list在某个时候初始化。

于 2012-10-10T15:12:28.013 回答