Android application crashes when get empty json data through parse.com

Below code works when I send json data from parse.com(neither empty nor null ). However, when uri or other tags are null or empty then application crashes. I have tried modifying my manifest class as a solution provided on stackoverflow. Didn't work for me. This is my class.

Help!

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 Manifest file

<!-- 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)

Answers


Yes sometimes it happens that the key values got null and we got the error due to null pointerException . SO the best way to do and check it is that

json.isNull( "field-name" )

so in this way you can check if the particular value exist or if it is null . Do what ever you want to do .


Replace the com.parse.ParsePushBroadcastReceiver in the Manifest file with the receiver class you are implementing and this will work.

 <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>

I think the crash is located at:

String data = intent.getExtras().getString(
                "com.parse.Data");

and this should be catched as

try{
    intent.getExtras().getString(
                "com.parse.Data")
} catch (Exception ex) {
    //...DoSomething...
}

But the thing is that I can not tell what is exactly crashing. So if you try to check if Json is null I think the parsing before checking if null will throw exception.


Need Your Help

htaccess eating up session variables?

php .htaccess

I have a problem with rewrite rule, it seems like it "eats" my set session variables.

Importing products straight to Magento's mysql database

mysql magento phpmyadmin

I have my own database with product information. I want to upload these products straight to Magento's mySQL database. I tried investigating database structure and it's extremely confusing. So far ...