0

I am using GCM to get values from Server. It worked properly when I was trying to get a single string. Now I have to get array of values using JSON. Here I have worked as below but its crashing now am not able to exactly find the error.

Please find the code and Log generated while implementing it for Array.

// receiving push messages
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    String latId;
    String lonId;       
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String markerArray = intent.getExtras().getString(
                "message");     
        JSONArray jResponse = null;     

        try {
            JSONObject jResult = new JSONObject(markerArray);
            jResponse = jResult.getJSONArray("user_position");              
            for(int i=0;i<jResponse.length();i++){                  
                JSONObject posInfo = jResponse.getJSONObject(i);                    
                latId = posInfo.getString("lat");
                lonId = posInfo.getString("lon");
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }           
        // Waking up mobile if it is sleeping
        WakeLocker.acquire(getApplicationContext());

        System.out.println("new message      " + latId);
        System.out.println("new message      " + lonId);
        /**
         * Take appropriate action on this message depending upon your app
         * requirement For now i am just displaying it on the screen
         * */   

    // Releasing wake lock
        WakeLocker.release();

    }

};

Log File

  java.lang.RuntimeException: Error receiving broadcast Intent { act=info.ae.su.DISPLAY_MESSAGE flg=0x10 (has extras) } in info.ae.su.MainActivity$1@41f0b070
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at info.ae.su.MainActivity$1.onReceive(MainActivity.java:296)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)
Munchoo
  • 313
  • 1
  • 7
  • 22

0 Answers0