简体   繁体   中英

Android - Alert Dialog force close

I want to display the alert dialog below but it force closes, im little bit confused about what the context should be. This dialog is shown when an image is clicked.

AlertDialog.Builder builder = new AlertDialog.Builder(
getApplicationContext(), R.style.TitleDialog);
builder.setTitle("Sorry for Inconvinience");
builder.setMessage("You need to install MX Player or VLC Media Player");
AlertDialog alert = builder.create();
alert.show();

I am a beginner any help will be appreciated full onclicklistener

ib2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isInternetPresent) {
                try {
                    startActivity(i2);
                } catch (Exception e) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
                    builder.setTitle("Sorry for Inconvinience");
                    builder.setMessage("You need to install MX Player or VLC Media Player, here is the link: ");
                    AlertDialog alert = builder.create();
                    alert.show();

                }
            } else {
                Toast.makeText(getApplicationContext(),
                        "Please check your Internet Connection.",
                        Toast.LENGTH_LONG).show();
            }
        }
    });
  AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
        builder.setTitle("Sorry for Inconvinience");
        buildersetMessage("You need to install MX Player or VLC Media Player");
       .show();

The above code should work. I don't know what

getApplicationContext(), R.style.TitleDialog);

is used for though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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