当我从parse.com发送json数据时,下面的代码工作(既不是空也不是null)。但是,当uri或其他标记为null或为空时,应用程序崩溃。我已经尝试修改我的清单类作为stackoverflow上提供的解决方案。没有为我工作。这是我的班级。
帮助!
public class PNReceiverClass extends ParsePushBroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
private static final int NOTIFICATION_ID = 1;
@SuppressLint("NewApi")
@Override
protected void onPushReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onPushReceive(context, intent);
try {
JSONObject json = new JSONObject(intent.getExtras().getString(
"com.parse.Data"));
// Log.d(TAG, json.getString("alert").toString());
String notificationTitle = null, notificationContent = null, uri=null;
if(json!=null) {
if (json.has("title") && json.has("alert") && json.has("uri")) {
notificationTitle = json.getString("title");
notificationContent = json.getString("alert");
uri = json.getString("uri");
// Customize your notification - sample code
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.vision_icon)
.setContentTitle(notificationTitle)
.setContentText(notificationContent);
NotificationManager mNotifyMgr = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(NOTIFICATION_ID, builder.build());
}
else {
Class<? extends Activity> cls = getActivity(context, intent);
Intent activityIntent;
if ((uri != null && !uri.isEmpty())
&& (notificationTitle != null && !notificationTitle.isEmpty())
&& (notificationContent != null && !notificationContent
.isEmpty())) {
activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
}
else {
activityIntent = new Intent(context, SplashActivity.class);
}
activityIntent.putExtras(intent.getExtras());
if (Build.VERSION.SDK_INT >= 16) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(activityIntent);
stackBuilder.startActivities();
} else {
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activityIntent);
}
}
}
else {
Toast.makeText(context, "Empty push!", Toast.LENGTH_LONG).show();
}
}
catch (JSONException e) {
Log.d(TAG, e.getMessage());
}
}
}
Android清单文件
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:protectionLevel="signature"
android:name="my.package.name.permission.C2D_MESSAGE"
/>
<uses-permission android:name="my.package.name.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="my.package.name" />
</intent-filter>
</receiver>
<receiver
android:name="my.package.name.PNReceiverClass"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="my.package.name.UPDATE_STATUS" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="my.package.name" />
</intent-filter>
</receiver>
logcat的:
08-28 11:33:31.646: E/AndroidRuntime(25486): java.lang.RuntimeException: Unable to start receiver com.parse.ParsePushBroadcastReceiver: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= flg=0x1000c000 (has extras) }
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2567)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.ActivityThread.access$1800(ActivityThread.java:161)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.os.Handler.dispatchMessage(Handler.java:102)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.os.Looper.loop(Looper.java:157)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-28 11:33:31.646: E/AndroidRuntime(25486): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 11:33:31.646: E/AndroidRuntime(25486): at java.lang.reflect.Method.invoke(Method.java:515)
08-28 11:33:31.646: E/AndroidRuntime(25486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-28 11:33:31.646: E/AndroidRuntime(25486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-28 11:33:31.646: E/AndroidRuntime(25486): at dalvik.system.NativeStart.main(Native Method)
08-28 11:33:31.646: E/AndroidRuntime(25486): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= flg=0x1000c000 (has extras) }
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.Instrumentation.execStartActivitiesAsUser(Instrumentation.java:1499)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.ContextImpl.startActivitiesAsUser(ContextImpl.java:1451)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.content.ContextWrapper.startActivitiesAsUser(ContextWrapper.java:356)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.content.ContextWrapper.startActivitiesAsUser(ContextWrapper.java:356)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:221)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:232)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:208)
08-28 11:33:31.646: E/AndroidRuntime(25486): at com.parse.TaskStackBuilderHelper.startActivities(TaskStackBuilderHelper.java:30)
08-28 11:33:31.646: E/AndroidRuntime(25486): at com.parse.ParsePushBroadcastReceiver.onPushOpen(ParsePushBroadcastReceiver.java:220)
08-28 11:33:31.646: E/AndroidRuntime(25486): at com.parse.ParsePushBroadcastReceiver.onReceive(ParsePushBroadcastReceiver.java:123)
08-28 11:33:31.646: E/AndroidRuntime(25486): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2552)
答案 0 :(得分:0)
是的,有时会发生键值变为null并且由于null pointerException而导致错误。因此,最好的方法是检查
json.isNull(“field-name”)
因此,您可以检查特定值是否存在或是否为null。做任何你想做的事 。
答案 1 :(得分:0)
将Manifest文件中的com.parse.ParsePushBroadcastReceiver替换为您正在实现的接收器类,这将起作用。
<receiver
android:name="your.package.name.ReceiverClassName"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
答案 2 :(得分:0)
我认为崩溃位于:
String data = intent.getExtras().getString(
"com.parse.Data");
这应该被视为
try{
intent.getExtras().getString(
"com.parse.Data")
} catch (Exception ex) {
//...DoSomething...
}
但事实是,我无法分辨出究竟是什么崩溃。因此,如果您尝试检查Json是否为null,我认为在检查null之前的解析是否会抛出异常。