0

我正在尝试在(Sherlock)的方法中设置一个ArrayAdapterfor a 。我得到了,但我认为布局和字符串数组的路径是正确的,因为当我直接为列表设置适配器时,会显示列表。ListViewonCreateDialog()DialogFragmentNullPointerExceptionAlertDialog.Builder

public class InputMyDialogFragment 
    extends SherlockDialogFragment 
    implements OnClickListener {

    private ListView listView;
    private ArrayAdapter<String> adapter;
    private String[] myItemArray;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        myItemArray = getResources().getStringArray(R.array.my_item_array);
        listView = (ListView) getActivity().findViewById(R.id.dialog_listView);
        adapter = new ArrayAdapter<String>(getActivity(), R.id.row_relativeLayout, myItemArray);
        listView.setAdapter(adapter);

        return new AlertDialog.Builder(getActivity())
            .setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, null))
            //.setAdapter(adapter, this)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
    }
}

有什么建议么?

编辑:

事实证明,行条目 XML 似乎是不正确的,因为我测试了下面提到的代码

adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice, myItemArray);

它奏效了。

这是我在教程中找到的自定义行条目的 XML(不再找到链接):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row_relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true" >

    <TextView
        android:id="@+id/row_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="left|center_vertical"
        android:singleLine="false"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small" />

    <CheckedTextView
        android:id="@+id/row_checkstate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="6dip"
        android:background="#00000000"
        android:checkMark="@android:drawable/btn_radio"
        android:checked="false"
        android:clickable="false"
        android:focusable="false" />

</RelativeLayout>

我不知道可能出了什么问题...

4

2 回答 2

2

您尝试在扩展布局之前找到您的列表。

在你的方法开始时试试这个:

View v = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
//if you only have one listview in your layout, v is your listview
// ListView lv = (ListView)v;
ListView lv = v.findById(R.id.dialog_listView);

然后,v在您的 return 语句中给出或夸大布局。

return new AlertDialog.Builder(getActivity())
        .setView(v)...
于 2013-07-15T12:03:43.670 回答
0

你应该在 onCreateView 方法中为 dialogFragment 使用你自己的布局

前任:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle    savedInstanceState)
{
    // Inflate the layout to use as dialog or embedded fragment     
    View v = inflater.inflate(R.layout.layout_frogos_password, container, false);

    Button buttonOk = (Button) v.findViewById(R.id.buttonForgotPasswordDialogOk);


    buttonOk.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            mListener.onDialogPositiveClick(ForgotPasswordDialog.this);             
        }
    });

    return v;
}

/** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    // The only reason you might override this method when using
    // onCreateView() is
    // to modify any dialog characteristics. For example, the dialog
    // includes a
    // title by default, but your custom layout might not need it. So here
    // you can
    // remove the dialog title, but you must call the superclass to get the
    // Dialog.
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    return dialog;
}
于 2013-07-15T11:50:15.113 回答