【发布时间】:2015-04-04 12:56:27
【问题描述】:
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;
}
}
这里是适配器类。当我使用它在 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;
}
}
【问题讨论】:
标签: android android-fragments nullpointerexception fragment listadapter