我正在使用第三方(Pushbots)API开发通知应用程序,几乎我已经完成了我的应用程序。我可以从第三方API接收通知。我将应用程序用户划分为两种类型,一种是ITTI,另一种是EXTERNAL用户。
如果我登录我的应用程序,因为ITTI用户意味着它的工作正常。如果我以外部用户身份登录,我可以从第三方API
接收状态栏中的通知,但问题是如果单击状态栏上的通知以加载EXTERNAL用户主要活动页面,它抛出NullPointerException
。但是对于ITTI用户,我可以接收,也可以打开ITTI
用户主要活动页面。
为了接收通知,我使用了BroadcastReceiver
onReceive()
方法。我的猜测是intent
onReceive()
方法中的空例外,以下是使用BroadcastReceiver接收通知的代码:
**ReceiverClass.java**
package com.example.itti_integration;
import java.util.HashMap;
import com.example.external.External;
import com.pushbots.push.Pushbots;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class CustomPushReceiver extends BroadcastReceiver{
private static final String TAG = "customPushReceiver";
private static final String MODE_PRIVATE = null;
SharedPreferences Notify_Pref;
String mm1, mm2;
String mm;
String Extmsg;
@Override
public void onReceive(Context con, Intent in) {
//String msg = null, qry = null;
// TODO Auto-generated method stub
try{
String action = in.getAction();
System.out.println(action+ " intent action");
Log.d(TAG, "action=" + action);
String itti_alias = null;
String alias= System.getProperty("alias", itti_alias);
String ealias = null;
String external = System.getProperty("extAlias", ealias);
System.out.println(alias + "ealias "+ external);
if(alias.equals("itti"))
{
// Handle Push Message when opened
if (action.equals(Pushbots.MSG_OPENED)) {
HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) in.getExtras().get(Pushbots.MSG_OPEN);
Log.w(TAG, "User clicked notification with Message: " + PushdataOpen.get("message") );
mm1=PushdataOpen.get("message").toString();
if(!LoadMsg.isActive() )
{
HashMap<?, ?> PushdataOpen1 = (HashMap<?, ?>) in.getExtras().get(Pushbots.MSG_RECEIVE);
Intent launch = new Intent(Intent.ACTION_MAIN);
launch.setClass(Pushbots.getInstance().appContext, LoadMsg.class);
launch.putExtra("message", mm1);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Pushbots.getInstance().appContext.startActivity(launch);
}
// Handle Push Message when received
}
else if(action.equals(Pushbots.MSG_RECEIVE))
{
HashMap<?, ?> Pushdata = (HashMap<?, ?>) in.getExtras().get(Pushbots.MSG_RECEIVE);
Log.w(TAG, "User Received notification with Message: " + Pushdata.get("message"));
mm=Pushdata.get("message").toString();
System.setProperty("mm", mm);
}
}
else
// Handle Push Message when opened
if (action.equals(Pushbots.MSG_OPENED)) {
HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) in.getExtras().get(Pushbots.MSG_OPEN);
Log.w(TAG, "User clicked notification with Message: " + PushdataOpen.get("message") );
mm2=PushdataOpen.get("message").toString();
System.setProperty("emsg", mm2);
if(!External.isActive() ){
HashMap<?, ?> PushdataOpen1 = (HashMap<?, ?>) in.getExtras().get(Pushbots.MSG_RECEIVE);
Intent launch1 = new Intent(Intent.ACTION_MAIN);
launch1.setClass(Pushbots.getInstance().appContext, External.class);
//launch.putExtra("message", mm1);
launch1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Pushbots.getInstance().appContext.startActivity(launch1);
}
// Handle Push Message when received
}
else if(action.equals(Pushbots.MSG_RECEIVE)){
HashMap<?, ?> Pushdata = (HashMap<?, ?>) in.getExtras().get(Pushbots.MSG_RECEIVE);
Log.w(TAG, "User Received notification with Message: " + Pushdata.get("message"));
mm1=Pushdata.get("message").toString();
System.setProperty("mm1", mm1);
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.toString());
}
}
}
这是我用来接收通知和根据用户类别进行网页加载的课程,我真的陷入了这一点,我不知道为什么会出现Null异常。
这是我的Logcat:
08-04 17:16:36.002: W/System.err(2913): java.lang.NullPointerException
08-04 17:16:36.002: W/System.err(2913): at com.example.itti_integration.CustomPushReceiver.onReceive(CustomPushReceiver.java:58)
08-04 17:16:36.014: W/System.err(2913): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
08-04 17:16:36.032: W/System.err(2913): at android.app.ActivityThread.access$1500(ActivityThread.java:130)
08-04 17:16:36.052: W/System.err(2913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
08-04 17:16:36.062: W/System.err(2913): at android.os.Looper.loop(Looper.java:137)
08-04 17:16:36.072: W/System.err(2913): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-04 17:16:36.072: W/System.err(2913): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 17:16:36.072: W/System.err(2913): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 17:16:36.072: W/System.err(2913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-04 17:16:36.072: W/System.err(2913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-04 17:16:36.222: W/System.err(2913): at dalvik.system.NativeStart.main(Native Method)
08-04 17:16:36.242: I/System.out(2913): java.lang.NullPointerException
我仍然无法弄清楚我错在哪里。请任何人建议我解决方案......