2017-11-21 31 views
0

내 문제는 내 선호 활동이 AppCompatPreferenceActivity에서 확장되어 있고 DialogFragment을 보여줄 필요가 있다는 것입니다. 나는 다음 작업을 수행 할 때 그래서 : 나는 getSupportFragmentManager() 방법을 사용할 수 없기 때문에클래스의 DialogFragment가 AppCompatPreferenceActivity에서 확장됩니다.

DialogFragmente dialogFragment= new MyDialogFragmentClass() 
DialogFragment.show(getSupportFragmentManager(),"My Dialog") 

오류가 발생합니다. 그래서 내 질문은 어떻게 내 클래스 AppCompatPreferenceActivity에서 확장하는 경우 해당 메서드를 사용할 수 있습니다.

대신 getFragmentManager()을 사용해 보았지만 둘 다 작동하지 않습니다.

이 있습니다 내 수업 :

public class Settings_acticity extends AppCompatPreferenceActivity { 
    private SwitchPreference banner_switch; 
    private SharedPreferences sharedPreferences; 
    private ListPreference interstial_ad; 

    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
     DialogFragment dialogFragment = new MyDialog(); 
     dialogFragment.show(getSupportFragmentManager(), "My dialog"); 
    } 
} 

그리고 DialogFragment 클래스 : 도움을

public class MyDialog extends DialogFragment { 


    public MyDialog(){ 
     // Required empty public constructor 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     View vista=getActivity().getLayoutInflater().inflate(R.layout.fragment_MyDialog,null); 
     setCancelable(false); 

     builder.setView(vista).setPositiveButton(R.string.aceptar, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       //Yes Code 
      } 
     }).setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
      //No code 
      } 
     }); 
     return builder.create(); 
    } 

} 

감사합니다!

+0

https://stackoverflow.com/questions/25887373/calling-dialogfragment-from을 -fragment-not-fragmentactivity –

+0

'오류가 발생합니다 '라는 오류가 표시됩니다. –

+0

링크가 내 질문을 해결하지 못했습니다. 주요 문제는 getSupportFragmentManager() 메서드가 필요하지만 AppCompatPreferenceActivity를 확장 할 수 있기 때문입니다. –

답변

1

아마도 지원 라이브러리에서 DialogFragment을 사용했기 때문일 수 있습니다.

그런 다음에 대화 표시 할 수 있습니다 android.support.v4.app.DialogFragment

대신 android.app.DialogFragment을 가져 와서 원래 하나를 사용하십시오 :

MyDialog myDialog = new MyDialog(); 
myDialog.show(getFragmentManager(), "My Dialog"); 
+0

감사합니다! DialogFragment 클래스에서 v4를 가져 오기 때문에 ... 메인 클래스에서 android.app.DialogFragment를 사용했을 때 오류가 발생했습니다. –

+0

당신을 환영합니다;) –