Domanda

I want to use Android Query library to load a fragment with a listview in order to display up to 5 photos keeping the aspect ratio. I have followed the piece of code provided in the demo app of AQ trying to adapt it with a fragment and not a view.

Here is the piece of code:

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

}

Problem is that the app crashes, here is the error:

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

The crash happens on this line:

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

Any idea on what i am doing wrong? I could not find an example of app with a fragment and listview of images as I want to do..

Thanks for your ideas!

È stato utile?

Soluzione

Found the issue!

Replaced

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

By

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

:)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top