Android 从BroadcastReceiver调用活动内部的方法

Android 从BroadcastReceiver调用活动内部的方法,android,broadcastreceiver,parse-platform,Android,Broadcastreceiver,Parse Platform,我正在使用Parse.com服务发送推送通知,并使用广播接收器接收消息。我想在我的activity from broadcast receiver中调用一个方法来显示Toast消息(实际上我想更新listview内容,但现在我只使用Toast来检查是否成功调用了该方法),而不调用activity 首先,我谨此陈辞: 但它会产生错误: 12-20 20:53:33.892: E/AndroidRuntime(14245): FATAL EXCEPTION: main 12-20 20:53:33.

我正在使用Parse.com服务发送推送通知,并使用广播接收器接收消息。我想在我的activity from broadcast receiver中调用一个方法来显示Toast消息(实际上我想更新listview内容,但现在我只使用Toast来检查是否成功调用了该方法),而不调用activity

首先,我谨此陈辞:

但它会产生错误:

12-20 20:53:33.892: E/AndroidRuntime(14245): FATAL EXCEPTION: main
12-20 20:53:33.892: E/AndroidRuntime(14245): java.lang.RuntimeException: Unable to start receiver event.planner.services.CustomReceiver: java.lang.NullPointerException
12-20 20:53:33.892: E/AndroidRuntime(14245):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at android.app.ActivityThread.access$1500(ActivityThread.java:130)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at android.os.Looper.loop(Looper.java:137)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at java.lang.reflect.Method.invokeNative(Native Method)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at java.lang.reflect.Method.invoke(Method.java:511)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at dalvik.system.NativeStart.main(Native Method)
12-20 20:53:33.892: E/AndroidRuntime(14245): Caused by: java.lang.NullPointerException
12-20 20:53:33.892: E/AndroidRuntime(14245):    at event.planner.services.CustomReceiver.onReceive(CustomReceiver.java:60)
12-20 20:53:33.892: E/AndroidRuntime(14245):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
12-20 20:53:33.892: E/AndroidRuntime(14245):    ... 10 more
12-20 20:53:35.883: I/Process(14245): Sending signal. PID: 14245 SIG: 9
12-20 20:53:41.322: E/Trace(15934): error opening trace file: No such file or directory (2)
12-20 20:53:41.615: E/com.parse.PushService(15934): The Parse push service cannot start because Parse.initialize has not yet been called. If you call Parse.initialize from an Activity's onCreate, that call should instead be in the Application.onCreate. Be sure your Application class is registered in your AndroidManifest.xml with the android:name property of your <application> tag.
MyApplication.java

public class CustomReceiver extends BroadcastReceiver {
    private static final String TAG = "MyCustomReceiver";
    public static final String ACTION = "com.example.UPDATE_STATUS";

    FrontLayout main = null;
    public void setMainActivityHandler(FrontLayout main){
        this.main=main;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String title="";
        String alert="";
        try {
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            Iterator<?> itr = json.keys();
            while (itr.hasNext()) {
                String key = (String) itr.next();
                if(key.equals("title"))
                    title = json.getString(key);
                if(key.equals("alert"))
                    alert = json.getString(key);
                else
                    continue;
            }
         } catch (JSONException e) {
            Log.w(TAG, "JSONException: " + e.getMessage());
         }

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",Locale.US);
        Date date = new Date();

        NotificationModel nm = new NotificationModel();
        nm.setTitle(title);
        nm.setAlert(alert);
        nm.setDateTime(dateFormat.format(date));

        NotificationDS nds = new NotificationDS(context);
        nds.insertNotification(nm);

        main.UpdateList();
    }
}
public class FrontLayout extends Activity 
{
   ....
   CustomReceiver receiver = null;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   ......

   receiver = new CustomReceiver();
        receiver.setMainActivityHandler(this);
        IntentFilter callInterceptorIntentFilter = new IntentFilter("com.example.UPDATE_STATUS");
        registerReceiver(receiver, callInterceptorIntentFilter);

   }

   public static void UpdateList()
   {
       Toast.makeText(MyApplication.getAppContext(), "Success!", Toast.LENGTH_SHORT).show();
   }
}
package event.planner.services;

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}
遵循以下步骤

Step 1:Declare a Interface class in that BroadcastReceiver

Step 2: Implement that interface in your Activity class...
示例代码:

类内---------

内部FrontLayout.class

CustomReceiver.setListener(this);  // otherwise null pointer occurs
最后实现该侦听器…

遵循以下步骤

Step 1:Declare a Interface class in that BroadcastReceiver

Step 2: Implement that interface in your Activity class...
示例代码:

类内---------

内部FrontLayout.class

CustomReceiver.setListener(this);  // otherwise null pointer occurs

最后实现侦听器…

在BroadcastReceiver中声明接口类会产生错误:“成员接口只能在顶级类或接口中定义”。然后我尝试创建一个新的接口类,在活动类中实现一个方法,并通过
MyInterface mi=newfrontlayout()调用该方法;mi.MyMethod()但仍然生成空指针。检查编辑的答案..如果有帮助,接受此答案..谢谢。在BroadcastReceiver中声明接口类会产生错误:“成员接口只能在顶级类或接口内定义”。然后我尝试创建一个新的接口类,在活动类中实现一个方法,并通过
MyInterface mi=newfrontlayout()调用该方法;mi.MyMethod()但仍然生成空指针。请检查编辑的答案。如果有帮助,请接受此答案。谢谢。
CustomReceiver.setListener(this);  // otherwise null pointer occurs