Dodi
Dodi

Reputation: 2269

null pointer exception calling a method

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)

Upvotes: 0

Views: 2881

Answers (5)

Shobhit Puri
Shobhit Puri

Reputation: 26007

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.

Upvotes: 0

Mohit
Mohit

Reputation: 11314

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());

Upvotes: 2

trungdinhtrong
trungdinhtrong

Reputation: 2674

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();              
}

Upvotes: 0

Erik
Erik

Reputation: 5119

Yea that's it you start an Activity like:

    Intent intent = new Intent(this, YourActivity.class);
    startActivity(intent);

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

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.

Upvotes: 4

Related Questions