gpt4 book ai didi

android - 传递结果失败 ResultInfo | java.lang.IllegalStateException : Can not perform this action after onSaveInstanceState 错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:31 25 4
gpt4 key购买 nike

我有一个简单的应用程序,首先我显示 MainActivity,然后在 MainActivity 可见之后我显示 TransparentActivity 然后 onClick 我杀死 TransparentActivity 然后我创建并显示对话框。在最后一步我得到错误

错误

Failure delivering result ResultInfo{who=null, request=1234, result=-1, data=Intent {  }} to activity {com.example.kylu.layout/com.example.kylu.layout.GuidePhotoAlbum}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

主要 Activity

public class MainActivity extends FragmentActivity
{
public static boolean flag=false;
private static int REQUEST_CODE = 1234;

@Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
}

@Override
protected void onResume() {
super.onResume();
//transparentShows only once
if(!flag)
{
Intent i = new Intent(this, TransparentTip.class);
startActivityForResult(i, REQUEST_CODE);
flag=true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (REQUEST_CODE == requestCode) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Show dialog", Toast.LENGTH_SHORT).show();
DialogChoiceActivity dialogChoiceActivity = new DialogChoiceActivity();
dialogChoiceActivity.show(getSupportFragmentManager(),"mydialog");
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Don't show dialog", Toast.LENGTH_SHORT).show();
}
}
}

透明提示

    public class TransparentTip extends Activity {

public static int countTransp=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.transparent_tip);
Button ok=(Button)findViewById(R.id.bToK);
}

public void buttonClick(View view)
{
if (view.getId() == R.id.bToK)
{
final Intent intent = new Intent();
setResult(Activity.RESULT_OK, intent);
finish();
}
}

对话框选择 Activity

public class DialogChoiceActivity extends DialogFragment {

LayoutInflater inflater;
View v;

public Dialog onCreateDialog(Bundle savedInstanceState) {

inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.dialog_email, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Email " + " 1/10");
builder..setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
});
return builder.create();
}
}

在 Transparent 不再可见后,我想在 MainActivity 上方显示 Dialog。我尝试在 Resume 上显示此对话框,但出现类似的错误

最佳答案

如果将此行放入 onActivityResult 中,它应该可以工作:

super.onActivityResult(requestCode,resultCode,intent);

因此,您的 onActivityResult 应该如下所示:

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode,resultCode,intent);
if (REQUEST_CODE == requestCode) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Show dialog", Toast.LENGTH_SHORT).show();
DialogChoiceActivity dialogChoiceActivity = new DialogChoiceActivity();
dialogChoiceActivity.show(getSupportFragmentManager(),"mydialog");
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Don't show dialog", Toast.LENGTH_SHORT).show();
}
}
}

关于android - 传递结果失败 ResultInfo | java.lang.IllegalStateException : Can not perform this action after onSaveInstanceState 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43935681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com