-1

我创建了一个自定义 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>
4

2 回答 2

1

您的列表为空,因为您试图从 Activity 布局而不是 Dialog 中获取视图。

尝试保留 Dialog 视图的引用并从那里获取它:

View dialogView = inflater.inflate(R.layout.confirm_lineup_layout, null);
// some code
list =  (ListView) dialogView.findViewById(R.id.list_confirm_lineup);
于 2014-04-17T12:40:58.390 回答
0

我更新你的方法

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

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

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.confirm_lineup_layout, null);
        builder.setView(v);

        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) v.findViewById(R.id.list_confirm_lineup);

        list.setAdapter(adapter); // HERE NullPointerException

        return builder.create();
    }

问题是您试图从活动中找到列表视图,但它实际上是在 alertbuilder 视图中。所以我只是改变它。

于 2014-04-17T12:44:21.013 回答