1

내 대화 상자에서 내 radiogroup의 라디오 버튼이 선택되었는지 확인하고 싶습니다. 나는 그것을 (이미 아래에서 볼 수 있듯이) 시도했지만이 줄에서 충돌했다 : RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup);과 nullpointerexception. 여기 Android : 대화 상자에서 radiogroup의 널 포인트 인식

내 코드입니다 :

class newLessonDialog extends DialogFragment { 
         @Override 
         public Dialog onCreateDialog(Bundle savedInstanceState) { 
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
          // Get the layout inflater 
          LayoutInflater inflater = getActivity().getLayoutInflater(); 

          // Inflate and set the layout for the dialog 
          // Pass null as the parent view because its going in the dialog layout 
          builder.setView(inflater.inflate(R.layout.new_lesson_dialog, null)) 
            // Add action buttons, 
            .setPositiveButton("Speichern", new DialogInterface.OnClickListener() { 
             @Override 
             public void onClick(DialogInterface dialog, int id) 
             { 
              EditText eF = (EditText)getDialog().findViewById(R.id.editFach); 
              fach = eF.getText().toString(); 
              EditText eR = (EditText)getDialog().findViewById(R.id.editRaum); 
              raum = eR.getText().toString(); 
              EditText eL = (EditText)getDialog().findViewById(R.id.editLehrer); 
              lehrer = eL.getText().toString(); 

              if (tag != null) 
              { 
               save(fach, raum, lehrer, tag, index); 
              } 
              else 
              { 
               Toast.makeText(getAppContext(), "Zuerst Wochentag auswählen!", Toast.LENGTH_SHORT); 
              } 
             } 
            }) 
            .setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int id) { 
              newLessonDialog.this.getDialog().cancel(); 
             } 
            }); 

          // This will get the radiogroup 
          RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup); 
          // This will get the radiobutton in the radiogroup that is checked 
          RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId()); 

          rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
          { 
           public void onCheckedChanged(RadioGroup rGroup, int checkedId) 
           { 
            // This will get the radiobutton that has changed in its check state 
            RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId); 
            // This puts the value (true/false) into the variable 
            boolean isChecked = checkedRadioButton.isChecked(); 
            // If the radiobutton that has changed in check state is now checked... 
            if (isChecked) 
            { 
             tag = checkedRadioButton.getText().toString(); 
            } 
           } 
          }); 

          return builder.create(); 
         } 


        } 

여기에 내 로그 캣입니다 :

대신 그냥이 같은 활동의 이름을 넣어 시도 getActivity의
08-14 16:16:23.749 7618-7618/de.nathan.android.droidschool D/AndroidRuntime: Shutting down VM 
08-14 16:16:23.749 7618-7618/de.nathan.android.droidschool W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41760700) 
08-14 16:16:23.779 7618-7618/de.nathan.android.droidschool E/AndroidRuntime: FATAL EXCEPTION: main 
     java.lang.NullPointerException 
     at de.nathan.android.droidschool.MainActivity$fragmentTab1$1$1newLessonDialog.onCreateDialog(MainActivity.java:303) 
     at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 
     at android.os.Handler.handleCallback(Handler.java:730) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5103) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     at dalvik.system.NativeStart.main(Native Method) 
08-14 16:16:23.789  438-665/? W/ActivityManager: Force finishing activity de.nathan.android.droidschool/.MainActivity 
08-14 16:16:24.309  438-454/? W/ActivityManager: Activity pause timeout for ActivityRecord{41ef07a8 u0 de.nathan.android.droidschool/.MainActivity} 
08-14 16:16:34.899  438-454/? W/ActivityManager: Activity destroy timeout for ActivityRecord{41ef07a8 u0 de.nathan.android.droidschool/.MainActivity} 
+0

그리고 'MainActivity.java : 303'은이 코드에서 정확히 어디에 있습니까? – g00dy

+0

onCreateDialog가 호출 된 후 getDialog를 호출하면 대화 상자가 null이되지 않습니다. getDialog는 onCreateDialog에 반환 된 대화 상자에 대한 참조를 가져 오려고합니다. 따라서 getDialog 코드를 프래그먼트 수명주기의 후반부로 이동하십시오. 예 : onResume – Tobrun

+0

[정확한 복제본] (http://stackoverflow.com/questions/8456143/dialogfragment-getdialog-returns-null) 1 년 6 개월 전부터. – lifeson106

답변

0

당신은 방법이 onCreateDialog에서 getDialog()을 (호출). 따라서 대화 상자가 아직 만들어지지 않았으며 Null 포인터가 있어야합니다.

대화 상자가 그때까지 만들어 졌으므로 onStart도 덮어 쓰고 대신보기를 설정해야합니다.

See this answer.

0

:

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this); 
1

내가 가진를 아무 것도 코드를 테스트하지만 대화 상자가 아직 생성되지 않았으므로 getDialog와 같은 소리가납니다.

View myView = inflater.inflate(R.layout.new_lesson_dialog); 
RadioGroup rGroup = (RadioGroup)myView.findViewById(R.id.radioGroup); 
//Do wathever you want with RadioGroup 

다음

builder.setView(myView) 

주의 : 팽창 전에 반드시 숙지하는 데 시간이 걸릴하시기 바랍니다 : http://www.doubleencore.com/2013/05/layout-inflation-as-intended/