0

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.

Rami
  • 7,879
  • 12
  • 36
  • 66
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – OneCricketeer Jan 12 '16 at 20:40
  • I know what is null pointer exception, but now it is like difference between activities and fragments and how to avoid these things by changing some of the code which is suitable for fragments not for activities.. :( –  Jan 12 '16 at 20:42
  • Well, you should include a bit more code and point to exactly which line the null pointer is at. There is no Volley code in your post – OneCricketeer Jan 12 '16 at 20:47
  • Well i think now i have a problem with controller, and for example in adapter here i get error : ImageLoader imageloader =.... so maybe there is an error in controller instance that is null... –  Jan 12 '16 at 21:00
  • 1
    @DntQuitPls Have you added the AppController.java class in AndroidManifest.xml to your tag using name property (like mentioned in the tutorial)? – Rami Jan 12 '16 at 21:06
  • Omg, totally forgot, my bad... I am very appreciated. –  Jan 12 '16 at 21:28

1 Answers1

1

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Rami
  • 7,879
  • 12
  • 36
  • 66