2014-02-22 82 views
0

我想要使用Android查詢庫加載帶有列表視圖的片段,以便最多顯示5張保持縱橫比的照片。我遵循AQ的演示應用程序中提供的代碼片段,試圖用片段而不是視圖來調整它。Android查詢 - 在片段中使用縱橫比在片段中加載圖像

下面是一段代碼:

package com.example.testaq; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import com.androidquery.AQuery; 

import android.app.Fragment; 
import android.app.FragmentManager; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.AdapterView.OnItemClickListener; 

public class PhotoFragment extends Fragment { 

    protected AQuery listAq; 

    public PhotoFragment() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_photo, container, false); 

     // Android Query - Initialization - used to load the pictures 
     AQuery listAq = new AQuery(getActivity(), rootView); 

     return rootView; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // If we need to do anything with the layout it is the place to do so... 
     final HashMap<String, String> myHashMap; 
     List<String> items = new ArrayList<String>(); 
     Button myAddPhoto; 
     ListView myListPhotos; 

     myAddPhoto=(Button) getActivity().findViewById(R.id.button_addphoto); 
     myListPhotos=(ListView) getActivity().findViewById(R.id.list_photos); 
     myHashMap = MainActivity.LAST_OUTPUT.get(MainActivity.LAST_ID); 

     // Loads the URL of the photos in a list (up to 5, in MainActivity variables) 
     if(!myHashMap.get(MainActivity.KEY_PHOTO).equals("")) items.add(MainActivity.DOMAIN + myHashMap.get(MainActivity.KEY_PHOTO)); 
     if(!myHashMap.get(MainActivity.KEY_PHOTO2).equals("")) items.add(MainActivity.DOMAIN + myHashMap.get(MainActivity.KEY_PHOTO2)); 
     if(!myHashMap.get(MainActivity.KEY_PHOTO3).equals("")) items.add(MainActivity.DOMAIN + myHashMap.get(MainActivity.KEY_PHOTO3)); 
     if(!myHashMap.get(MainActivity.KEY_PHOTO4).equals("")) items.add(MainActivity.DOMAIN + myHashMap.get(MainActivity.KEY_PHOTO4)); 
     if(!myHashMap.get(MainActivity.KEY_PHOTO5).equals("")) items.add(MainActivity.DOMAIN + myHashMap.get(MainActivity.KEY_PHOTO5)); 

     ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(), R.layout.list_photo, items){ 

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

       if(convertView == null){ 
        convertView = getActivity().getLayoutInflater().inflate(R.layout.list_photo, null); 
       } 

       String url = getItem(position); 

       AQuery aq = listAq.recycle(convertView); 
       aq.id(R.id.tb).image(url, true, true, 0, 0, null, 0, AQuery.RATIO_PRESERVE); 

       return convertView; 
      } 
     }; 

     listAq.id(R.id.list_photos).adapter(aa); 

    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
    } 

} 

問題是,應用程序崩潰,這裏是錯誤:

02-22 17:08:35.339: E/AndroidRuntime(14259): FATAL EXCEPTION: main 02-22 17:08:35.339: E/AndroidRuntime(14259): java.lang.NullPointerException 02-22 17:08:35.339: E/AndroidRuntime(14259): at com.example.testaq.PhotoFragment.onStart(PhotoFragment.java:94) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.app.Fragment.performStart(Fragment.java:1719) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:913) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.app.BackStackRecord.run(BackStackRecord.java:682) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.os.Handler.handleCallback(Handler.java:725) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.os.Handler.dispatchMessage(Handler.java:92) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.os.Looper.loop(Looper.java:137) 02-22 17:08:35.339: E/AndroidRuntime(14259): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-22 17:08:35.339: E/AndroidRuntime(14259): at java.lang.reflect.Method.invokeNative(Native Method) 02-22 17:08:35.339: E/AndroidRuntime(14259): at java.lang.reflect.Method.invoke(Method.java:511) 02-22 17:08:35.339: E/AndroidRuntime(14259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-22 17:08:35.339: E/AndroidRuntime(14259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-22 17:08:35.339: E/AndroidRuntime(14259): at dalvik.system.NativeStart.main(Native Method) 02-22 17:12:15.761: I/Process(14259): Sending signal. PID: 14259 SIG: 9

的崩潰發生在這條線:

listAq.id(R.id.list_photos).adapter(aa); 

關於我做錯什麼想法?我無法找到應用程序的例子,因爲我想要的圖像的片段和列表視圖。

感謝您的想法!

回答

1

發現問題!

替換

AQuery listAq = new AQuery(getActivity(), rootView); 

通過

listAq = new AQuery(getActivity(), rootView); 

:)