目前我正在研究显示对话框的 fragment Activity ,但我真的不明白为什么我不能显示我的对话框。所以这是我从 android 开发者页面使用的示例:
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
并用它来调用对话框:
public void confirmFireMissiles() {
DialogFragment newFragment = new mainActivity();
newFragment.show(getSupportFragmentManager(), "missiles");
}
现在 getSupportFragmentManager 总是变成红色,当我专注于它时,它会给我一个错误,提示无法解析方法“show(?,java.lang.String)”和无法解析方法“getSupportFragmentManager()”。我现在处于 panic 模式,我不知道该怎么办。我已经导入了所需的内容,但我不知道为什么仍然会发生这种情况。
最佳答案
试试下面的方法。
点击按钮时说。
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getFragmentManager(), "missiles");
编辑:
如果您的 min sdk 低于 api 11,请导入以下内容。扩展 FragmentActivity
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
使用
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
关于android - 丢失 fragment Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038741/