简体   繁体   English

如何使用AlertDialog进行活动?

[英]How to intent activity use AlertDialog?

public void Home (View view){
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("Back");
    dialog.setIcon(R.drawable.logo);
    dialog.setCancelable(true);
    dialog.setMessage("Do you want to go back?");
    dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }

    });

    dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    dialog.show();
}

Now I stay at pageOne.java when I click button "Home". 现在,当我单击“主页”按钮时,我将停留在pageOne.java上。 I want to go back to MainActivity.java but I get an error. 我想回到MainActivity.java,但出现错误。

在此处输入图片说明

Your usage of this is a problem: this用法是一个问题:

new Intent(this, MainActivity.class);

In that scope, it thinks this is referring to the anonymous inner class DialogInterface.OnClickListener . 在此范围内,它认为this是指匿名内部类DialogInterface.OnClickListener So instead, you should use a reference to your outer class instance like HomeActivity.this (or whatever your activity is called). 因此,您应该使用对外部类实例的引用,例如HomeActivity.this (或任何您调用的活动)。

new Intent(HomeActivity.this, MainActivity.class);

"this" you used in ClickListener refer to the Click Event Listener. 您在ClickListener中使用的“ this”是指Click Event Listener。

But need to add is YourActivity.this, and if it is a fragment call getActivity() instead of "this". 但是需要添加的是YourActivity.this,如果它是片段,则调用getActivity()而不是“ this”。

I hope it will be OK for you. 希望您一切都好。

  public void Home (View view){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Back");
        dialog.setIcon(R.drawable.logo);
        dialog.setCancelable(true);
        dialog.setMessage("Do you want to go back?");
        dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(pageOne.this, MainActivity.class);
                startActivity(intent);
            }

        });

        dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        dialog.show();
    }

您可以尝试Intent intent = new Intent(pageOne.this, MainActivity.class);

Use getApplicationContext or Activity.this. 使用getApplicationContext或Activity.this。 It is good to use this method. 最好使用这种方法。

 Intent in=new Intent(PageOne.this,MainActivity. this);
 startActivity(in);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM