0

my application just crashes whenever it tries to run the alertdialog. I'm not to sure the proper way to do these but what I am doing is, creating a Java Class called 'AboutActivity' which contains the method 'popup()' ideally to create the AlertDialog. In my main activity I am creating the object and calling the method.

'popup()' in AboutActivity:

public class AboutActivity extends DialogFragment
{
    public void popup()
    {
        new AlertDialog.Builder(getActivity())
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("About Us")
                .setMessage("Blah Blah Blah")
                .setCancelable(true)
                .setPositiveButton("Ok", null)
                .show();
    }
}

Main activity:

 public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.nav_About:{
                AboutActivity about = new AboutActivity();
                about.popup();
            }
}
Andrew Ricci
  • 475
  • 5
  • 21

3 Answers3

0

Main activity:

Just create the dialog there... There is no point in having a Fragment just to load that

 new AlertDialog.Builder(MainActivity.this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("About Us")
            .setMessage("Blah Blah Blah")
            .setCancelable(true)
            .setPositiveButton("Ok", null)
            .show();

Your problem is that getActivity() is unassigned for a brand new, unattached Fragment

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Send a logcat, but I guess it's because your getActivity() is null. If you want to have it inside your method just pass your activity there.

'popup()' in AboutActivity:

public class AboutActivity extends DialogFragment {
  public void popup(Activity activity) {
    new AlertDialog.Builder(activity)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("About Us")
    .setMessage("Blah Blah Blah")
    .setCancelable(true)
    .setPositiveButton("Ok", null)
    .show();
  }
}

Main activity:

public boolean onOptionsItemSelected(MenuItem item) {
  switch(item.getItemId()){
    case R.id.nav_About:{
      AboutActivity about = new AboutActivity();
      about.popup(this);
  }
}
Kia
  • 124
  • 1
  • 1
  • 10
0

First of all replace your alert dialog code with this:

new AlertDialog.Builder(AboutActivity.this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("About Us")
            .setMessage("Blah Blah Blah")
            .setCancelable(true)
            .setPositiveButton("Ok", null)
            .show();

This always works for me and I think your problem may be that you are using getActivity() which might not be the context, so use AboutActivity.this like I did in the example above.

Second, if I am correct, you are trying to go to the AboutActivity from the MainActivity, so you need to use an intent.

Replace this:

AboutActivity about = new AboutActivity();
about.popup();

with this:

Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
startActivity(intent);

If you replace you code with these two, you should be good to go!

Hope this helps!

Appafly
  • 656
  • 2
  • 6
  • 24