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

时间:2014-09-18 12:14:52

标签: android methods nullpointerexception broadcastreceiver

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

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

AndroidManifest:

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

MainActivity:

    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(&#34; MY_LOG&#34;,&#34;&#34; +&#34; REGISTERED&#34;)放在BroadcastReceiver的onReceive方法中,它确实出现了。< / p>

我把这段代码放了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)

1 个答案:

答案 0 :(得分:2)

如果您的活动中定义了broadcastReceiver,您可以尝试以下操作:

MainActivity.this.TestMethod();

那应该有用

希望它有所帮助。