Android 片段:为列表设置适配器给出 null

Android Fragment: setting Adapter for list gives null

我在使用 DialogFragment 时遇到了一些问题;当我尝试设置适配器时,出现 nullPointerException。当我在正常 Activity 中尝试此操作时,一切正常。 这是代码:

import java.util.LinkedList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ListView;

public class DayList extends DialogFragment {

    static String[] dayListArray = makeList();
    ListView list;

    Context context;
    DayList(Context context)
    {
        this.context = context;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.list, null, false));
        //list = (ListView)inflater.findViewById(R.id.listView);
        //ListView list = (ListView)getActivity().findViewById(R.id.listView);
        list.setAdapter(new Adapter(context));
        builder.setIcon(R.drawable.search_new);
        builder.setTitle("Szukaj");
        builder.setPositiveButton("Zapisz", null);
        builder.setNegativeButton("Anuluj", null);

        return builder.create();
    }

    static String[] makeList()
    {   
        int i;
        int ii;
        LinkedList<String> dayList = new LinkedList<String>();

        for (i = 0; i < 12; i++)
        {
            String month = Day.monthArrayNominative[i];
            Integer day = 0;
            for (ii = 1; ii < Day.monthDays[i]+1;ii++)
                {
                day = ii;
                dayList.add(month + " " + day.toString());
                }
        }
        dayListArray = dayList.toArray(new String[dayList.size ()]);

        return dayListArray;
    }

    static boolean[] checkedList(){
        int i;
        boolean[] checkedList = new boolean[366];

        for (i = 0; i < 366; i++)
        {
            checkedList[i] = true;
        }

        return checkedList;
    }
}

这是适配器 class。当我使用它在 MainActivity:

中设置时效果很好
public class Adapter extends BaseAdapter
{

    ArrayList<SingleRow> list = new ArrayList<SingleRow>();
    Context context;
    View row;
    Adapter(Context context)
    {
        Log.w("Test","ListAdapter run!");
        this.context = context;


        String[] days = DayList.dayListArray;
        boolean[] checks = DayList.checkedList();
        for(int i=0; i < 2;i++)
        {
            Log.w("Test","Checking");
            Log.w("Test",days[i]);
            list.add(new SingleRow(days[i], checks[i]));
        }
    }

    @Override
    public int getCount() {
        Log.w("Test","RUN");
        re

turn lis

t.size();
    }

    @Override
public Object getItem(int position) {
    Log.w("Test","RUN");
    return list.get(position);
}

@Override
public long getItemId(int position) {
    Log.w("Test","RUN");
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    Log.w("Test","RUN");
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    View row = inflater.inflate(R.layout.row, null, false);

        TextView day = (TextView) row.findViewById(R.id.textList);
        CheckBox checked = (CheckBox) row.findViewById(R.id.checkBox);

        SingleRow temp = list.get(position);

        day.setText(temp.day);
        checked.setChecked(temp.checked);
        return row;
    }
}

class SingleRow
{
    String day;
    boolean checked;

    SingleRow(String day, boolean checked){
        this.day = day;
        this.checked = checked;
    }
}

您的成员变量 list 未初始化,在 null 上调用方法会导致 NPE。

改变这个

builder.setView(inflater.inflate(R.layout.list, null, false));
//list = (ListView)inflater.findViewById(R.id.listView);
//ListView list = (ListView)getActivity().findViewById(R.id.listView);

类似于

View v = inflater.inflate(R.layout.list, null, false);
list = (ListView)v.findViewById(R.id.listView);
builder.setView(v);

给你NPE,因为你注释掉了ListView的初始化:

 LayoutInflater inflater = getActivity().getLayoutInflater();
 View view = inflater.inflate(R.layout.list, null, false)
 builder.setView(view);
 list = (ListView)view.findViewById(R.id.listView);