空指针异常调用一个方法指针、异常、方法

2023-09-12 05:56:08 作者:▍凉冬空巷°

我有2个活动,我的应用程序,在第二个我有code关于Android的动作栏图标被点击时运行。在第一个活动我也有同样的动作栏菜单项目,我想再次把这个约的方法,但是当我点击,我有空指针异常。谁能帮助?

这是在第二活动定义的方法 - JokeDetailsActivity

 公共无效aboutMe(){
    AlertDialog.Builder对话框=新AlertDialog.Builder(JokeDetailsActivity.this);
    dialog.setTitle(关于);
    dialog.setMessage(你好!我是...,这个应用程序的创造者。
               +如果发现请自由地给我发电子邮件的任何错误。+
                \ñ......
               );
    dialog.setPositiveButton(OK,新DialogInterface.OnClickListener(){

           @覆盖
           公共无效的onClick(DialogInterface对话,诠释它){
               dialog.cancel();

           }
       });
       dialog.show();
}
 

当我把它的第一个活动。

 案例R.id.action_about:

        JokeDetailsActivity JD =新JokeDetailsActivity();
        jd.aboutMe();
        返回true;
    }
 
使用Java 8 Optional避免空指针异常

这就是我得到的错误

  09-12 20:11:42.748:E / AndroidRuntime(1032):致命异常:主要
09-12 20:11:42.748:E / AndroidRuntime(1032):显示java.lang.NullPointerException
09-12 20:11:42.748:E / AndroidRuntime(1032):在android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:140)
09-12 20:11:42.748:E / AndroidRuntime(1032):在android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
09-12 20:11:42.748:E / AndroidRuntime(1032):在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
09-12 20:11:42.748:E / AndroidRuntime(1032):在android.app.AlertDialog $生成器< INIT>(AlertDialog.java:360)
09-12 20:11:42.748:E / AndroidRuntime(1032):在ie.myjokes.JokeDetailsActivity.aboutMe(JokeDetailsActivity.java:293)
09-12 20:11:42.748:E / AndroidRuntime(1032):在ie.myjokes.CategoryActivity.onOptionsItemSelected(CategoryActivity.java:140)
09-12 20:11:42.748:E / AndroidRuntime(1032):在android.app.Activity.onMenuItemSelected(Activity.java:2548)
 

解决方案

没有,不这样做

  JokeDetailsActivity JD =新JokeDetailsActivity();
 

您所得到的错误,在下面的行,因为你没有正确的上下文

  AlertDialog.Builder对话框=新AlertDialog.Builder(JokeDetailsActivity.this);
 

您正试图把它在一个活动,但使用上下文的另一个活动

您有几个选项

1简单地把其他活动创建此功能和公正     调用它的活动用正确的上下文( ActivityName.this )

2创建一个单独的类,所有这些活动可以调用使用此功能,并通过适当的上下文这个类。

3将这个方法在 BaseActivity 和你的活动扩展该 BaseActivity ,并把那里的方法。

我想起了#4

您也可以创建一个单独的活动,像 AboutActivity ,处理/显示任何你想要的,并给它一个对话主题中加入以下行<活性标记清单

 安卓主题=@安卓风格/ Theme.Dialog
 

然后你刚开始的活动无论你需要的。

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)

解决方案

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.