【问题标题】:Is there a way to call a method from MainActivity inside BroadCastReceiver onReceive?有没有办法从 BroadCastReceiver onReceive 中的 MainActivity 调用方法?
【发布时间】:2014-09-18 12:14:51
【问题描述】:

有没有办法从 BroadcastReceiver 调用 MainActivity 中的方法?

我想要做的就是能够跳过带有状态栏通知的歌曲。

AndroidManifest:

<receiver 
    android:name="MainActivity$switchButtonListener" />

主活动:

    resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);         

    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_remoteviews);

    Notification mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.img_btn_next)
    .setContentTitle("TEST")        
    .setWhen(System.currentTimeMillis())
    .setDefaults(Notification.DEFAULT_SOUND)
    .setAutoCancel(false)
    .setContent(notificationView).build();

    //this is the intent that is supposed to be called when the 
    //button is clicked
    Intent switchIntent = new Intent(this, switchButtonListener.class);
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);

    notificationView.setOnClickPendingIntent(R.id.closeOnFlash, pendingSwitchIntent);

    NotificationManager.notify(1, mBuilder);

.....

......


//Method
    public void TestMethod() {  
        Play(2);
    }

......

.....


public static class switchButtonListener extends BroadcastReceiver {
    private MainActivty mainActivity;
    @Override
    public void onReceive(Context context, Intent intent) {

        mainActivity = new MainActivty();
        mainActivity.TestMethod();      
    }

}

我将这个 Log.e("MY_LOG", "" + "REGISTERED") 放在 BroadcastReceiver 的 onReceive 方法中,它确实出现了。

我放置了这段代码 Play(2);在我的 MainActivity Oncreate 方法中,它运行良好。然而,

当我放置相同的代码 Play(2);在 TestMethod 中,它在下面给了我一个 NullPointerException .....这是怎么回事?

LogCat:

E/AndroidRuntime(889): java.lang.RuntimeException: Unable to start receiver main.activity.MainActivity$switchButtonListener: java.lang.NullPointerException

有人可以帮忙吗?

提前谢谢你。

编辑:添加更多 logcat

09-18 06:35:43.864: E/AndroidRuntime(889):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
09-18 06:35:43.864: E/AndroidRuntime(889):  at android.app.ActivityThread.access$1500(ActivityThread.java:141)
09-18 06:35:43.864: E/AndroidRuntime(889):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
09-18 06:35:43.864: E/AndroidRuntime(889):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 06:35:43.864: E/AndroidRuntime(889):  at android.os.Looper.loop(Looper.java:137)
09-18 06:35:43.864: E/AndroidRuntime(889):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-18 06:35:43.864: E/AndroidRuntime(889):  at java.lang.reflect.Method.invokeNative(Native Method)
09-18 06:35:43.864: E/AndroidRuntime(889):  at java.lang.reflect.Method.invoke(Method.java:525)
09-18 06:35:43.864: E/AndroidRuntime(889):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-18 06:35:43.864: E/AndroidRuntime(889):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 06:35:43.864: E/AndroidRuntime(889):  at dalvik.system.NativeStart.main(Native Method)
09-18 06:35:43.864: E/AndroidRuntime(889): Caused by: java.lang.NullPointerException
09-18 06:35:43.864: E/AndroidRuntime(889):  at main.activity.MainActivity.play(MainActivity.java:402)
09-18 06:35:43.864: E/AndroidRuntime(889):  at main.activity.MainActivity.TestMethod(MainActivity.java:661)
09-18 06:35:43.864: E/AndroidRuntime(889):  at main.activity.MainActivity$switchButtonListener.onReceive(MainActivity.java:648)
09-18 06:35:43.864: E/AndroidRuntime(889):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)

【问题讨论】:

  • 您能发布更多您的 Logcat 吗?您发布的行下方应该有一些行...
  • 毕竟,您应该能够从接收器中调用 MainActivity.this.TestMethode(),因为 switchButtonListener 是一个内部类。
  • 是静态类,所以不让我添加 MainActivity.this.TestMethode();它必须是静态类,否则 BroadCastReceiver 会抛出错误。谢谢

标签: android methods nullpointerexception broadcastreceiver


【解决方案1】:

如果您的 broadcastReceiver 在您的 Activity 中定义,您可以尝试以下操作:

MainActivity.this.TestMethod();

应该可以的

希望对你有帮助。

【讨论】:

  • 我把那个代码放进去,但它说没有 MainActivity 类型的封闭实例在范围内是可访问的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多