Android Fragment:设置适用于列表的适配器为null

时间:2015-02-04 20:36:13

标签: android android-fragments nullpointerexception fragment listadapter

我在DialogFragment方面遇到了一些问题;当我试图设置适配器时,我得到了nullPointerException。当我在正常活动中尝试这个时,一切都很清楚。 这是代码:

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

这是Adapter类。当我使用它在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;
    }
}

2 个答案:

答案 0 :(得分:0)

您的成员变量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);

答案 1 :(得分:0)

它给你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);