4

我有一个简单的应用程序,首先我显示 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

主要活动

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();
            }
        }

对话选择活动

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();
    }
}

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

4

2 回答 2

8

如果您将此行放入您的 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();
        }
    }
}
于 2017-05-12T13:47:53.497 回答
0

您可以使用 dialogChoiceActivity.show(getFragmentManager(),"mydia‌​log");

此行有语法错误有两个点 builder..setNegativeButton("Exit", new DialogInterface.OnClickListener() {

应该像这样 builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {

于 2017-05-12T11:19:04.447 回答