我创建了一个自定义 DialogFragment。它包含一个listView,但是当列表调用setAdapter 时会出现NullPointerException。我不明白为什么。这是代码:
public class LineUpConfirm extends DialogFragment {
private ListView list;
private List<ItemLineUpConfirm> pList = new ArrayList<ItemLineUpConfirm>();
private AdapterListConfirmLineUp adapter;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.confirm_lineup_layout, null));
pList.add(new ItemLineUpConfirm("P", "C. Abbiati","MIL","INT"));
pList.add(new ItemLineUpConfirm("D", "C. Papastapopulos","SAS","VER"));
pList.add(new ItemLineUpConfirm("C", "Cacca","SAM","GEN"));
adapter = new AdapterListConfirmLineUp(CurrentContext.getContext(),
R.layout.item_confirm_lineup, pList);
list = (ListView) getActivity().findViewById(R.id.list_confirm_lineup);
list.setAdapter(adapter); // HERE NullPointerException
return builder.create();
}
}
这是我的 Dialogfragment 的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/title_progressdialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Formazione creata"
android:layout_margin="5dp" />
<ListView
android:id="@+id/list_confirm_lineup"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:divider="#575555"
android:dividerHeight="1dp" />
<Button
android:id="@+id/button_confirm_lineup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Conferma"
android:layout_gravity="center" />
</LinearLayout>