user4229770
user4229770

Reputation:

fragment listview custom adapter error

Hello I tried to implement listview in my fragment, I used this tutorial.

With a single activity it works fine for me, but unfortunately when I want to implement with fragment it crashes when it being passed to my custom adapter, with error on my log and i think the problem is in controller which instance null when i use fragments and try to get getmImageLoader(), but with activity single it is okay...

So maybe guys You could help me a little bit since I am new to android. here is my code.

Also I inherit ListFragment in fragment and BaseAdapter on Adapter :

fragment code:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =  inflater.inflate(R.layout.fragment_fridge, container, false);

    listView = (ListView)v.findViewById(R.id.list_item1);
    adapter=new Adapter(getActivity(),array);
    listView.setAdapter(adapter);

fragment view(only list which is in framelayout):

     <!-- TODO: Update blank fragment layout -->
<ListView
    android:id="@+id/list_item1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="5dp"
    android:listSelector="@drawable/list_row_select"/>

Controller:

 public class AppController extends Application {
public static final String TAG= AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
    super.onCreate();
    mInstance=this;
}
public static synchronized AppController getmInstance(){
    return mInstance;
}
public RequestQueue getmRequestQueue() {
    if(mRequestQueue==null){
        mRequestQueue= Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}
public ImageLoader getmImageLoader(){
    getmRequestQueue();
    if(mImageLoader==null){
        mImageLoader=new ImageLoader(this.mRequestQueue,new BitmapCache());
    }
    return this.mImageLoader;
}

and Adapter:

public class Adapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Activity activity;
    private List<Item> items;
    ImageLoader imageLoader= AppController.getmInstance().getmImageLoader();
    public Adapter(Activity activity,List<Item> items){
        this.activity=activity;
        this.items=items;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(inflater==null){
            inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if(convertView ==null){
            convertView=inflater.inflate(R.layout.custom_layout,null);
        }
        if(imageLoader==null)
            imageLoader=AppController.getmInstance().getmImageLoader();
        NetworkImageView imageView= (NetworkImageView) convertView.findViewById(R.id.image_view);
        TextView title= (TextView) convertView.findViewById(R.id.tv_title);
        TextView rate= (TextView) convertView.findViewById(R.id.tv_rate);
        TextView genre= (TextView) convertView.findViewById(R.id.tv_genre);
        TextView year= (TextView) convertView.findViewById(R.id.tv_year);
        //getting data for row
        Item item=items.get(position);
        imageView.setImageUrl(item.getImage(), imageLoader);
        //title
        title.setText(item.getTitle());
        //rate
        rate.setText(String.valueOf(item.getRate()));
        String genreStr="";
        for(String str: item.getGenre()){
            genreStr +=str + ",";
        }
        genreStr = genreStr.length() >0 ? genreStr.substring(0, genreStr.length() - 2) : genreStr;
        genre.setText(genreStr);
        //year
        year.setText(String.valueOf(item.getYear()));

        return convertView;
    }
}

error

java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.example.marius.mykitchen.Controllers.AppController.getmImageLoader() on a null object reference

rest of the code imo does not matter so much. Thanks for Your help and time in advance.

Upvotes: 0

Views: 594

Answers (1)

Rami
Rami

Reputation: 7929

For future users,

The problem was skipping "step 11" of the tutorial:

Add the AppController.java class in AndroidManifest.xml to your <application> tag using name property to execute this class on application start.

<application
        android:name="info.androidhive.customlistviewvolley.app.AppController" ../>

Which cause the null value of mInstance in AppController.java.

So calling AppController.getmInstance().getmImageLoader(); will throw a NullPointerException because AppController.getmInstance() will always return a null object.

Upvotes: 1

Related Questions