0

我正在尝试使用对话框向数据库添加值。但是,我似乎无法从布局中获取值,因为它们总是为空。这是我的代码:

public class WlAddDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(com.mutoor.autoreply.R.layout.whitelistadd, null));
    builder.setPositiveButton(com.mutoor.autoreply.R.string.add, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d("Dialog", "Starting dialog");
            Context c = getActivity().getApplicationContext();
            Log.d("Dialog", "Context Received");
            WlHelper wl = new WlHelper(c);
            Log.d("Dialog", "Created Whitelist Helper");
            TextView eName = (TextView) getActivity().findViewById(R.id.entryName);
            TextView eNumber = (TextView) getActivity().findViewById(R.id.entryNumber);
            Log.d("Dialog", "Name = " + eName.getText().toString() + " , Number = " + eNumber.getText().toString());
            wl.createEntry(eName.getText().toString(), eNumber.getText().toString());
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(com.mutoor.autoreply.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    return builder.create();
}

这是我的输出:

01-02 22:23:23.350: D/Dialog(26471): Starting dialog
01-02 22:23:23.350: D/Dialog(26471): Context Received
01-02 22:23:23.358: D/Dialog(26471): Created Whitelist Helper
01-02 22:23:23.358: D/AndroidRuntime(26471): Shutting down VM
01-02 22:23:23.358: E/AndroidRuntime(26471): FATAL EXCEPTION: main
01-02 22:12:57.373: E/AndroidRuntime(25973): java.lang.NullPointerException
01-02 22:23:23.358: E/AndroidRuntime(26471): at com.mutoor.autoreply.WlAddDialog$1.onClick(WlAddDialog.java:36)

任何帮助表示赞赏。

4

4 回答 4

3

利用

TextView eName = (TextView)view.findViewById(R.id.entryName);
TextView eNumber = (TextView)view.findViewById(R.id.entryNumber);

如果您正在为 Dialog 使用自定义布局,则获取 TextView 值

编辑:将您的代码更改为:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

     View view = inflater.inflate(com.mutoor.autoreply.R.layout.whitelistadd, null);
    builder.setView(view);
            TextView eName = (TextView)view.findViewById(R.id.entryName);
            TextView eNumber = (TextView)view.findViewById(R.id.entryNumber);

    builder.setPositiveButton(com.mutoor.autoreply.R.string.add, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d("Dialog", "Starting dialog");
            Context c = getActivity().getApplicationContext();
            Log.d("Dialog", "Context Received");
            WlHelper wl = new WlHelper(c);
            Log.d("Dialog", "Created Whitelist Helper");

            Log.d("Dialog", "Name = " + eName.getText().toString() + " , Number = " + eNumber.getText().toString());
            wl.createEntry(eName.getText().toString(), eNumber.getText().toString());
            dialog.dismiss();
        }
    });
于 2013-01-03T06:34:32.247 回答
1

如果两个 TextView

TextView eName = (TextView) getActivity().findViewById(R.id.entryName);
TextView eNumber = (TextView) getActivity().findViewById(R.id.entryNumber);

是对话框布局的一部分,那么它们应该是,

TextView eName = (TextView) dialog.findViewById(R.id.entryName);
TextView eNumber = (TextView) dialog.findViewById(R.id.entryNumber);
于 2013-01-03T06:34:16.207 回答
0

你这样:

TextView eName = (TextView)layout.findViewById(R.id.entryName);
TextView eNumber = (TextView)layout.findViewById(R.id.entryNumber);

您需要定义layout.findViewById()以从对话框布局访问视图,这就是您收到空指针错误的原因。试试看,我认为这是唯一的问题。

于 2013-01-03T06:40:00.090 回答
0

看起来您正在从活动的布局而不是对话框的布局中查找文本框。我希望您在对话框中搜索 TextView 并使用那里提供的值来完成您的数据库事务。

于 2013-01-03T06:33:53.163 回答