将适配器设置为listview时,应用程序崩溃

时间:2014-06-19 07:43:16

标签: android android-listview

我的片段包含ListView。对于此ListView,我制作了自定义list item layout和自定义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;
}

我的适配器类:

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



}

然而,当我设置适配器时它会崩溃!我收到了nullpointerexception错误。

  

06-19 09:39:48.662:E / AndroidRuntime(29153):致命异常:主要   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(原生方法)

1 个答案:

答案 0 :(得分:1)

您未在mObjects中设置adapter值,因此您的适配器类将如下所示

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



    }

还可以从songList method.i.e传递一些onCreateView值到适配器。

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