简体   繁体   English

Android:AlertDialog不显示

[英]Android : AlertDialog doesn't show

I just started learning Android programming and got problem : My alert dialog's don't show up . 我刚刚开始学习Android编程,但遇到了问题: My alert dialog's don't show up

My idea : When app have been launched, it automatically checks if device is connected to internet, and gives suggestions (By alert dialog's). 我的想法:启动应用程序后,它将自动检查设备是否已连接到互联网,并给出建议(通过警报对话框)。

I really hope, that some one have solution, because I was looking in many tutorials, but every where was code that starts with onClickListener (Button) 我真的希望有人能找到解决方案,因为我在看很多教程,但是每个地方都以onClickListener (Button)开头的代码

Code Snippet: 代码段:

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkConnection();

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}


void checkConnection() {
    final ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this);

    if (wifi.isConnectedOrConnecting ()) {
        // Do nothing
    } else if (mobile.isConnectedOrConnecting ()) {

        connectionAlert.setMessage("We recommend to use wifi, enable it?");
        connectionAlert.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(SplashActivity.this,"Wifi has been enabled!",Toast.LENGTH_LONG).show();
            }
        });

        connectionAlert.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        connectionAlert.show();

    } else {

        connectionAlert.setMessage("Please, enable internet connection!");
        connectionAlert.setNeutralButton("Wifi", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(SplashActivity.this,"Wifi has been enabled!",Toast.LENGTH_LONG).show();
            }
        });

        connectionAlert.setNeutralButton("Mobile Data", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(SplashActivity.this,"Mobile Data has been enabled!",Toast.LENGTH_LONG).show();
            }
        });

        connectionAlert.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        connectionAlert.show();

    }
}

} }

Add context. 添加上下文。

public class SplashActivity extends AppCompatActivity {

private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;

    checkConnection();

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

/////// ///////

Change this line: 更改此行:

AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this); 

To: 至:

AlertDialog.Builder connectionAlert = new AlertDialog.Builder(context);

i think the issue because you call finish on the splash activity ! 我认为这个问题是因为您要求在启动活动上完成! why you have to finish it although you need to show alert dialog on its context ! 为什么您必须完成它,尽管您需要在上下文中显示警报对话框!

check your logcat you will find this 检查你的logcat,你会发现这个

YourActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40704090 that was originally added here YourActivity泄漏了最初在此处添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40704090

you can remove finish activity .. and move finish activity action to be one of the dialog buttons actions. 您可以删除完成活动..,并将完成活动动作移动为对话框按钮动作之一。

The problem is that you are creating an AlertDialog with the SplashActivity context, but you are finishing your SplashActivity right after and the context where the AlertDialog belongs no longer exists. 问题在于您正在使用SplashActivity上下文创建AlertDialog ,但是SplashActivity您将完成SplashActivity ,并且AlertDialog所属的上下文不再存在。

Considering that you want to move to your second activity, MainActivity , only before you choose Enable when you are showing this message We recommend to use wifi, enable it? 考虑到您只想在显示此消息时选择“ Enable ,然后再转到第二个活动MainActivityWe recommend to use wifi, enable it? . What you can do is something like this: 您可以执行以下操作:

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        checkConnection();
    }


    void checkConnection() {
        final ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this);

        if (wifi.isConnectedOrConnecting()) {
            // Do nothing
        } else if (mobile.isConnectedOrConnecting()) {

            connectionAlert.setMessage("We recommend to use wifi, enable it?");
            connectionAlert.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(SplashActivity.this, "Wifi has been enabled!", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            });

            connectionAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });

            connectionAlert.show();

        } else {

            connectionAlert.setMessage("Please, enable internet connection!");
            connectionAlert.setNeutralButton("Wifi", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(SplashActivity.this, "Wifi has been enabled!", Toast.LENGTH_LONG).show();
                }
            });

            connectionAlert.setNeutralButton("Mobile Data", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(SplashActivity.this, "Mobile Data has been enabled!", Toast.LENGTH_LONG).show();
                }
            });

            connectionAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });

            connectionAlert.show();

        }
    }
}

There are a lot of options here. 这里有很多选择。 But the important thing is that actually you are showing the AlertDialog but you are not being able to see it because you are starting another activity and finishing the SplashActivity , destroying the context ( this ) where the AlertDialog was first instantiated new AlertDialog.Builder(this); 但是重要的是,实际上您正在显示AlertDialog但是却看不到它,因为您正在启动另一个活动并完成SplashActivity ,破坏了AlertDialog首先实例化的上下文( thisnew AlertDialog.Builder(this); .

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

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