TextView值继续为null

时间:2013-01-03 06:30:58

标签: android dialog

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

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 个答案:

答案 0 :(得分: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();
        }
    });

答案 1 :(得分:1)

如果两个TextViews

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

是Dialog布局的一部分,然后它们应该是,

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

你这样:

  

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

您需要定义layout.findViewById()才能从对话框布局访问视图,这就是为什么会出现nullpointer错误。尝试我认为这是唯一的问题。