我有DialogFragment
自定义布局,其中包含EditText。我试图在'ADD'上获得EditText值点击但是我得到了NullPointerException
并且它崩溃了。
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
final View view = getActivity().getLayoutInflater().inflate(R.layout.add_note_dialog_fragment, null);
alertDialogBuilder.setView(view);
alertDialogBuilder.setTitle("Add New Note: ");
alertDialogBuilder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText noteTextEditText = (EditText) view.findViewById(R.id.notesText);
EditText noteTitleEditText = (EditText) view.findViewById(R.id.noteTitle2);
Editable noteText = noteTextEditText.getText();
// ^^^ HERE I GET NULL POINTER EXCEPTION
String y = noteText.toString();
String noteTitle = noteTitleEditText.getText().toString();
DatabaseHelper.addInABSTRACT_NOTES(noteText, noteTitle);
Toast.makeText(getActivity(), "ADDED ..", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
请建议我如何让它运作? 提前谢谢。
logcat的:
07-08 00:08:47.030: E/AndroidRuntime(16500): FATAL EXCEPTION: main
07-08 00:08:47.030: E/AndroidRuntime(16500): Process: com.shumail.newsroom, PID: 16500
07-08 00:08:47.030: E/AndroidRuntime(16500): java.lang.NullPointerException
07-08 00:08:47.030: E/AndroidRuntime(16500): at com.g_node.gca.abstracts.AddNoteDialogFragment$1.onClick(AddNoteDialogFragment.java:70)
07-08 00:08:47.030: E/AndroidRuntime(16500): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-08 00:08:47.030: E/AndroidRuntime(16500): at android.os.Handler.dispatchMessage(Handler.java:102)
07-08 00:08:47.030: E/AndroidRuntime(16500): at android.os.Looper.loop(Looper.java:136)
07-08 00:08:47.030: E/AndroidRuntime(16500): at android.app.ActivityThread.main(ActivityThread.java:5086)
07-08 00:08:47.030: E/AndroidRuntime(16500): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 00:08:47.030: E/AndroidRuntime(16500): at java.lang.reflect.Method.invoke(Method.java:515)
07-08 00:08:47.030: E/AndroidRuntime(16500): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-08 00:08:47.030: E/AndroidRuntime(16500): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-08 00:08:47.030: E/AndroidRuntime(16500): at dalvik.system.NativeStart.main(Native Method)
布局XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/noteTitleLabel" android:text="Note Title: "
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="@+id/noteTitle2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Enter Note Title..."
android:gravity="top"
android:layout_below="@+id/noteTitleLabel"
android:singleLine="true" />
<TextView
android:id="@+id/noteTextLabel" android:text="Note Content: "
android:layout_width="wrap_content" android:layout_below="@+id/noteTitle2" android:layout_height="wrap_content" />
<EditText
android:id="@+id/noteText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:hint="Enter Note Content..."
android:lines="5"
android:gravity="top"
android:layout_below="@+id/noteTextLabel"
android:singleLine="false" />
答案 0 :(得分:1)
问题在于ID。我错误地写了R.id.notesText
,而它应该是R.id.noteText