我有一个Activity,我需要在这个Activity中打开一个DialogFragment,以前使用just.DialogFragment是可以的。
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)){
FragmentManager fragmentManager = getSupportFragmentManager();
InfoLocalizacao infoLocalizacao = new InfoLocalizacao();
infoLocalizacao.show(fragmentManager, "InfoLocalizacao");"InfoLocalizacao");
...
使用这个方法只需要使用FragmentManager对象,但是,目前,这个方法返回这个。
java.lang.IllegalStateException: Fragment com.example.agrolucros2019.frags_e_actvs.venda.AddAnuncioActivity.InfoLocalizacao must be a public static class to be properly recreated from instance state.
这是我的DialogFragment代码。
public class InfoLocalizacao extends DialogFragment{
@NotNull
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("A Localização é importante");
builder.setIcon(R.drawable.ic_location_on_24dp);
builder.setMessage(R.string.avisoVenda)
.setPositiveButton("Estou Ciente", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), ":)", Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
到现在为止,我得到的是让InfoLocalizacao类成为静态的,它工作得很好,但是,对于静态的类,我不能使用非静态的方法,比如Toast.makeText,我不得不从我的代码中删除。
保持这样的。
public static class InfoLocalizacao extends DialogFragment{
@NotNull
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("A Localização é importante");
builder.setIcon(R.drawable.ic_location_on_24dp);
builder.setMessage(R.string.avisoVenda)
.setPositiveButton("Estou Ciente", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Toast.makeText(getApplicationContext(), ":)", Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}