Davlog
Davlog

Reputation: 2248

App crashes when setting adapter to listview

My fragment contains a ListView. For this ListView I have made a custom list item layout and a custom ListAdapter.

private DSongListAdapter adapter;
private ArrayList<Song> songList;
private View rootView;
private ListView listView;

@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
    rootView = inflater.inflate(R.layout.fragment_song, container, false);
    listView = (ListView) rootView.findViewById( R.id.playlist_listview );

    songList = new ArrayList<Song>();
    adapter = new DSongListAdapter(getActivity(), songList);
    listView.setAdapter(adapter); //Crashes now!

    return rootView;
}

My adapter class :

public class DSongListAdapter extends ArrayAdapter<Song> {

    Context mContext;
    List<Song> mObjects;

    public DSongListAdapter(Context context, List<Song> songList ) {
        super(context, R.layout.list_item, songList );
        this.mContext = context;
    }

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

        View itemView = convertView;
        if( itemView != null ){
            itemView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.list_item, parent, false);

            TextView titleLabel = (TextView) itemView.findViewById(R.id.titleLabel);
            if( titleLabel != null )
                titleLabel.setText( mObjects.get(position).getTitle() );
        }

        return convertView;
    }



}

However, when I set the adapter it crashes! I get a nullpointerexception error.

06-19 09:39:48.662: E/AndroidRuntime(29153): FATAL EXCEPTION: main 06-19 09:39:48.662: E/AndroidRuntime(29153): java.lang.NullPointerException 06-19 09:39:48.662: E/AndroidRuntime(29153): at com.davlog.musicplayer.SongFragment.onCreateView(SongFragment.java:31) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.app.Fragment.performCreateView(Fragment.java:1699) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.app.BackStackRecord.run(BackStackRecord.java:682) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.os.Handler.handleCallback(Handler.java:725) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.os.Handler.dispatchMessage(Handler.java:92) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.os.Looper.loop(Looper.java:137) 06-19 09:39:48.662: E/AndroidRuntime(29153): at android.app.ActivityThread.main(ActivityThread.java:5306) 06-19 09:39:48.662: E/AndroidRuntime(29153): at java.lang.reflect.Method.invokeNative(Native Method) 06-19 09:39:48.662: E/AndroidRuntime(29153): at java.lang.reflect.Method.invoke(Method.java:511) 06-19 09:39:48.662: E/AndroidRuntime(29153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 06-19 09:39:48.662: E/AndroidRuntime(29153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 06-19 09:39:48.662: E/AndroidRuntime(29153): at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 828

Answers (1)

Giru Bhai
Giru Bhai

Reputation: 14408

You are not setting mObjects values in adapter
So your adapter class will look like this

  public class DSongListAdapter extends ArrayAdapter<Song> {

        Context mContext;
        List<Song> mObjects;

        public DSongListAdapter(Context context, List<Song> songList ) {
            super(context, R.layout.list_item, songList );
            this.mContext = context;
            this.mObjects = songList;
        }


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

            ViewHolder holder;
            if( convertView == null ){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
                holder.titleLabel = (TextView) convertView.findViewById(R.id.titleLabel);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            holder.titleLabel.setText(mObjects.get(position).getTitle());
            return convertView;
        }

        class ViewHolder {
            TextView titleLabel;
        }



    }

And also pass some songList value to adapter from onCreateView method.i.e.

public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
    rootView = inflater.inflate(R.layout.fragment_song, container, false);
    listView = (ListView) rootView.findViewById( R.id.playlist_listview );

    songList = new ArrayList<Song>();
   // Add songList values here
    adapter = new DSongListAdapter(getActivity(), songList);
    listView.setAdapter(adapter); //Crashes now!

    return rootView;
}

Upvotes: 1

Related Questions