onBackPressed的createDialog,但我的应用程序显示错误关闭应用程序

时间:2018-06-11 11:07:37

标签: java android android-layout android-studio

我为onBackPressed创建了一个新的布局名称dialog_exit但是当我安装并打开时,我的应用程序无法打开并显示错误关闭app 请查看我的整个代码并指导我如何解决此问题 这是我的主要活动代码

public class MainActivity extends AppCompatActivity {

    public Dialog mDialog;
    public Button mDialogyes, mDialogno;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createDialog();
    }

    private void createDialog() {
        mDialog = new Dialog(this);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(R.layout.dialog_exit);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);
        mDialogyes = (Button) findViewById(R.id.yes);
        mDialogno = (Button) findViewById(R.id.no);

        mDialogyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
                finish();
                System.exit(0);

                mDialogno.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });
            }

        });

    }
    @Override
    public void onBackPressed() {
        mDialog.show();
    }
}

这是我的布局代码截图,因为

stackoverflow not allow me to add more code that why sharing image

3 个答案:

答案 0 :(得分:2)

更新了createDialog函数的代码

 private void createDialog() {
    mDialog = new Dialog(this);
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mDialog.setContentView(R.layout.dialog_exit);
    mDialog.setCanceledOnTouchOutside(true);
    mDialog.setCancelable(true);
    mDialogyes = (Button) mDialog.findViewById(R.id.yes);
    mDialogno = (Button) mDialog.findViewById(R.id.no);

    mDialogyes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            finish();
            System.exit(0);

        }

    });

    mDialogno.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mDialog.dismiss();
        }
    });

}

答案 1 :(得分:1)

try this code 
       mDialogyes = (Button)mDialogyes. findViewById(R.id.yes);
        mDialogno = (Button)mDialogyes. findViewById(R.id.no);

    mDialogyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
                finish();
                System.exit(0);


            }

        });

           mDialogno.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });

答案 2 :(得分:0)

你必须这样做

mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);