0

I have 2 activities in my app, in the second one I have the code to run when the "about" android action bar icon is clicked. In the first activity I have the same action bar menu items and I want to call this "about" method again, however when I click that, I have null Pointer exception. Anyone help ?

this is the method defined in the second activity - JokeDetailsActivity

public void aboutMe(){
    AlertDialog.Builder dialog = new AlertDialog.Builder(JokeDetailsActivity.this);
    dialog.setTitle("About");
    dialog.setMessage("Hello! I'm ..., the creator of this application."
               +"If there is any bug found please freely e-mail me. "+
                "\n ...."
               );
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

           @Override
           public void onClick(DialogInterface dialog, int which) {
               dialog.cancel();

           }
       });
       dialog.show();              
}

when I call it in the first activity

case R.id.action_about:

        JokeDetailsActivity jd = new JokeDetailsActivity();
        jd.aboutMe();
        return true;
    }

thats the error I'm getting

09-12 20:11:42.748: E/AndroidRuntime(1032): FATAL EXCEPTION: main
09-12 20:11:42.748: E/AndroidRuntime(1032): java.lang.NullPointerException
09-12 20:11:42.748: E/AndroidRuntime(1032):     at            android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:140)
09-12 20:11:42.748: E/AndroidRuntime(1032):     at           android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
09-12 20:11:42.748: E/AndroidRuntime(1032):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
09-12 20:11:42.748: E/AndroidRuntime(1032):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
09-12 20:11:42.748: E/AndroidRuntime(1032):     at ie.myjokes.JokeDetailsActivity.aboutMe(JokeDetailsActivity.java:293)
09-12 20:11:42.748: E/AndroidRuntime(1032):     at ie.myjokes.CategoryActivity.onOptionsItemSelected(CategoryActivity.java:140)
09-12 20:11:42.748: E/AndroidRuntime(1032):     at android.app.Activity.onMenuItemSelected(Activity.java:2548)
Dodi
  • 2,201
  • 4
  • 30
  • 39

5 Answers5

4

No, don't do this

JokeDetailsActivity jd = new JokeDetailsActivity();

You are getting the error in the following line because you don't have the correct Context.

AlertDialog.Builder dialog = new AlertDialog.Builder(JokeDetailsActivity.this);

You are trying to call it in one Activity but use the Context of another Activity

You have a few options

1 Simply recreate this function in your other Activity and just call it in that Activity with the proper Context (ActivityName.this)

2 Create a separate class that all of these Activities can call to use this function and pass the proper Context to that class.

3 Put this method in a BaseActivity and have your Activities extend this BaseActivity and place the method there.

I remembered #4

You could also create a separate Activity, like AboutActivity, to handle/show whatever you want and give it a Dialog Theme by adding the following line to the <activity tag in your manifest

android:theme="@android:style/Theme.Dialog"

then you just start that Activity from wherever you need.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
2

What is this

 JokeDetailsActivity jd = new JokeDetailsActivity();
 jd.aboutMe();

If you want to start Activity you should do this using intent

You should never instantiate Activities

Create a seperate Class pass Context and create your aboutMe() method there and then reuse it

OR

Alternatively you can create it as static method in it

public static void aboutMe(Context mContext){
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle("About");
    dialog.setMessage("Hello! I'm ..., the creator of this application."
               +"If there is any bug found please freely e-mail me. "+
                "\n ...."
               );
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

           @Override
           public void onClick(DialogInterface dialog, int which) {
               dialog.cancel();

           }
       });
       dialog.show();              
}

then use it like

JokeDetailsActivity.aboutMe(getApplicationContext());
Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
0

Declare that method as static and call it as JokeDetailsActivity.aboutMe(yourActivityContext). Don't create an object of activity and remember to pass the context, which you can then use to create the dialog. @Hi-Tech KitKat Android has givn more detailed answer.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • No, don't create a static method on an activity. – 323go Sep 12 '13 at 20:39
  • @323go okay. Curious to know that are there any repercussions ? – Shobhit Puri Sep 12 '13 at 20:44
  • First off, the above code, obviously, wouldn't work, as there's no enclosing instance of the `Activity` available to provide a `Context`. The code above should be moved to the activity from which it is called; if it's called from more than one spot, it should be a static method outside of an activity. – 323go Sep 12 '13 at 21:17
  • @323go I agree that context needs to be passed for it to work. As you said, its a good design to move it into some common `Util` file etc, and make it static if its called at more than one place( Although if context passed it still could be accessed from the first activiy). So the issue with answer is that context was not passed and bad design, right? Fair enough :) – Shobhit Puri Sep 12 '13 at 21:26
0

Yea that's it you start an Activity like:

    Intent intent = new Intent(this, YourActivity.class);
    startActivity(intent);
Erik
  • 5,039
  • 10
  • 63
  • 119
0

You cannot instantiate an Activity class like that and use it. The Activity creation is handled by Android system and it will need to get things like Context and stuff (which is the thing that you dont have, and you do use when you, for example, instantiate the AltertDialog builder, hence the null pointer exception). I would suggest that you make that method aboutMe a static method, and pass in a Context object to it:

public static void aboutMe(Context context){
    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    dialog.setTitle("About");
    dialog.setMessage("Hello! I'm ..., the creator of this application."
               +"If there is any bug found please freely e-mail me. "+
                "\n ...."
               );
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

           @Override
           public void onClick(DialogInterface dialog, int which) {
               dialog.cancel();

           }
       });
       dialog.show();              
}
trungdinhtrong
  • 2,604
  • 2
  • 17
  • 26
  • You can instantiate activities – Trikaldarshiii Sep 12 '13 at 20:38
  • but it will through Runtime Exception – Trikaldarshiii Sep 12 '13 at 20:39
  • By the way, before people give bad feedback about static method - in this case it is probably a good case for a static method because it does little almost nothing relating to specific data of a class or anything like that. – trungdinhtrong Sep 12 '13 at 20:40
  • Also, I cannot add comment on other stuff yet, but this is not the case where you will need to use Intent at all. If you use Intent you will start another activity. The PO does not want that. He just wants to stay in his second activity, but show a dialog about him – trungdinhtrong Sep 12 '13 at 20:44
  • I am just talking about word you use CanNot but you can which causes exception nothing more – Trikaldarshiii Sep 12 '13 at 20:47
  • No, I get it :). I was just commenting about stuffs that other people says in other answers, or in the comment of the question. I dont think this question should be voted down. This is not a bad question, it is indeed a well-formed question. The PO might be a bit inexperience, but we all once were. – trungdinhtrong Sep 12 '13 at 20:51
  • ya may be ppl are more rude here – Trikaldarshiii Sep 12 '13 at 20:52