Search code examples
androidandroid-broadcast

How to update TextView with onReceive method from BroadcastReceiver


I created an activity that receives a GCM message and displays it's content. I was able to use a broadcastReceiver to grab the message in the activity but it crashes when i try to append the message to the TextView using tvMessage.append(). This is the code for the receiver inside the display activity.

private final BroadcastReceiver handleDiscussionMessage = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        WakeLockerUtility.acquire(getApplicationContext());
        SharedPreferences sharedPref =
                 getSharedPreferences(SHAREDPREF_LOCATION, 0);
                 SharedPreferences.Editor editor = sharedPref.edit();
                 editor.putString("receivedMessage", newMessage);
                 editor.commit();

        tvMessage.append("<- " + newMessage + "\n");
        Toast.makeText(getApplicationContext(), "New Message Received: " + newMessage, Toast.LENGTH_LONG).show();
        WakeLockerUtility.release();
    }

};

This is what my logcat shows when the application crashes:

08-16 16:10:08.701: E/AndroidRuntime(24024): FATAL EXCEPTION: main
08-16 16:10:08.701: E/AndroidRuntime(24024): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.example.miingle.DISPLAY_MESSAGE flg=0x10 (has extras) } in com.example.miingle.DiscussionsActivity$1@40dc11f8
08-16 16:10:08.701: E/AndroidRuntime(24024):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:794)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at android.os.Handler.handleCallback(Handler.java:605)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at android.os.Looper.loop(Looper.java:154)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at android.app.ActivityThread.main(ActivityThread.java:4945)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at java.lang.reflect.Method.invokeNative(Native Method)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at java.lang.reflect.Method.invoke(Method.java:511)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at dalvik.system.NativeStart.main(Native Method)
08-16 16:10:08.701: E/AndroidRuntime(24024): Caused by: java.lang.NullPointerException
08-16 16:10:08.701: E/AndroidRuntime(24024):    at com.example.miingle.DiscussionsActivity$1.onReceive(DiscussionsActivity.java:247)
08-16 16:10:08.701: E/AndroidRuntime(24024):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:781)
08-16 16:10:08.701: E/AndroidRuntime(24024):    ... 9 more

Any help will be appreciated. thanks.


Solution

  • Why not pull the message out from Shared Preferences since I can see that you have stored the message there? Also you need to ensure that tvMessage is not null as Tarun pointed out.