【问题标题】:call to non-static method from a static method crash从静态方法崩溃调用非静态方法
【发布时间】:2016-10-04 02:34:05
【问题描述】:

我试图从静态方法调用非静态方法,但应用程序崩溃,我不知道为什么会发生这种情况。如果您能帮助我了解为什么会发生这种情况以及如何解决它,我会很高兴。

静态函数-

    public static void myPlace(String place, String titles) {

    if (place == "[]") {
        // Not found Skip
    } else {
        Log.d("placea - ", place);
        Log.d("titles - ", titles);

        BackgroundProcess b = new BackgroundProcess();
        b.addNotification();

    }

非静态函数 -

    public void addNotification() {

    NotificationCompat.Builder builder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Notifications Example")
                    .setContentText("This is a test notification");

    Intent notificationIntent = new Intent(this, MyNotification.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(FM_NOTIFICATION_ID, builder.build());
}

日志-

FATAL EXCEPTION: AsyncTask #3
                                                              Process: alon.myplace, PID: 18679
                                                              java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                  at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                  at java.lang.Thread.run(Thread.java:818)
                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
                                                                  at android.content.ComponentName.<init>(ComponentName.java:77)
                                                                  at android.content.Intent.<init>(Intent.java:4160)
                                                                  at alon.myplace.BackgroundProcess.addNotification(BackgroundProcess.java:238)
                                                                  at alon.myplace.BackgroundProcess.myPlace(BackgroundProcess.java:224)
                                                                  at alon.myplace.PlacesDisplayTask.doInBackground(PlacesDisplayTask.java:33)
                                                                  at alon.myplace.PlacesDisplayTask.doInBackground(PlacesDisplayTask.java:13)
                                                                  at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)

感谢您的帮助。

【问题讨论】:

  • if (place == "[]") {???请使用equals(...) 作为字符串。
  • 例如,if ("[]".equals(place) {
  • @HovercraftFullOfEels 谢谢,但这不是我的问题
  • 也许不是现在,但是一旦您解决了当前的问题,它成为您的问题!另外,您看到了什么“错误”?
  • 请发布崩溃日志。

标签: java android android-studio static crash


【解决方案1】:

问题在于您传入的上下文

Intent notificationIntent = new Intent(this, MyNotification.class);

this 为空。

请检查您是如何获取上下文的。它应该是一个有效的上下文并且不应该为空。

【讨论】:

  • 谢谢,但我不确定实现这一目标的方法是什么,我怎样才能获得正确的上下文?
  • addNotification() 方法在哪里?
  • addNotification() 与 myPlace 函数属于同一类。名为 BackgroundProcess 的后台类中的类。
  • 你需要将上下文从activity/fragment一路传递到这个类,然后使用它。
  • 哇,看来这个方法效率不高,不过还是谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多