Edittextにusernameとpasswordを入力して、新規登録ボタンを押したところ、
下のコードの一番下に、NullPointerExceptionが出ます。
AlertDialogで、EditTextの中身を取得できません。なぜなのでしょうか?
アドバイスをいただけないでしょうか?
どうぞよろしくお願いします。
java
1//MainActivityです。 2 // カスタムビューを設定 3 LayoutInflater inflater = (LayoutInflater)this.getSystemService( 4 LAYOUT_INFLATER_SERVICE); 5 final View layout =inflater.inflate(R.layout.dialog_contact_us,(ViewGroup)findViewById(R.id.layout_root)); 6 7 8 // アラーとダイアログ を生成 9 AlertDialog.Builder builder = new AlertDialog.Builder(this); 10 builder.setTitle("ダイアログタイトル"); 11 builder.setView(layout); 12builder.setPositiveButton("新規登録", new DialogInterface.OnClickListener() { 13 public void onClick(DialogInterface dialog, int which) { 14 15 EditText getusername2 = (EditText) findViewById(R.id.username); 16 EditText getpassword2 = (EditText) findViewById(R.id.password); 17 18//下の行がエラーの出ている163行目です 19 String getusername =getusername2.getText().toString(); 20 String password =getpassword2.getText().toString(); 21
java
1//エラーメッセージです。 2FATAL EXCEPTION: main 3 java.lang.NullPointerException 4 at com.example.android.sample.near.LocationActivity$1.onClick(LocationActivity.java:163) 5 at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 6at android.os.Handler.dispatchMessage(Handler.java:99) 7 at android.os.Looper.loop(Looper.java:213) 8 at android.app.ActivityThread.main(ActivityThread.java:4786) 9 at java.lang.reflect.Method.invokeNative(Native Method) 10 at java.lang.reflect.Method.invoke(Method.java:511) 11 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 12 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 13 at dalvik.system.NativeStart.main(Native Method)
xml
1//dialog_contact_us.xmlです。 2<?xml version="1.0" encoding="utf-8"?> 3<LinearLayout 4xmlns:android="http://schemas.android.com/apk/res/android" 5android:id="@+id/layout_root" 6android:layout_width="match_parent" 7android:layout_height="match_parent" 8android:orientation="vertical" 9android:padding="10dp" 10android:text="各項目を入力してください"> 11 12<TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="ID" 16 /> 17<EditText 18 android:id="@+id/username" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:hint="usernameを入力してください" 22 android:inputType="text" 23 /> 24<TextView 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="Password" 28 /> 29<EditText 30 android:id="@+id/password" 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:hint="passwordを入力してください。" 34 android:inputType="textPassword" 35 /> 36 37</LinearLayout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。