我正在尝试使用对话框向数据库添加值。但是,我似乎无法从布局中获取值,因为它们总是为空。这是我的代码:
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)
任何帮助表示赞赏。