2012-10-10 97 views
0

我有獲得由自定義適配器(延遲加載)應該顯示一些文本和image.SO我建了一個列表片段和自定義適配器低於實現this.Code填充列表片段但它扔錯誤顯示(登錄貓)的Android ListFragment和lazyloader適配器錯誤

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; 
} 

logcat的:

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) 

回答

1

使用getActivity()而不是fragmentActivity(在Activity可以在onAttachnull)。還要確保list在某個時間點被初始化。

+0

名單已初始化....在變化融通您提供..the listfragment只是顯示加載...實際上本身並沒有填充ListView –

+0

基本上,你應該做的是:(1)初始化適配器,並將其綁定到什麼在'onActivityCreated()'中用'setListAdapter(adapter)'調用'ListView'。你也可以調用'setListShown(false)'來表示數據尚未加載。 (2)讓你的'AsyncTask'產生某種容器對象的保存數據和可以被綁定到適配器(即'Cursor')。 (3)中的數據綁定到'onPostExecute適配器()'(和調用'setListShown(真)'如果你FALSE'之前將其設置爲')。 –

+0

太感謝你了.....這完全曾爲.... –