简体   繁体   中英

how to change image in imagebutton when sms is received

i've been working with my thesis and i've been stuck with receiving sms. i was able to pass the message body from the broadcastreceiver to activity and toast the message in the activity. however, there are multiple imagebuttons and a specific image button will change based from the message body. my code looks something like:

public String message;
.
.
.

//in my onCreate
.
.
.
Intent intent = getIntent();
message=intent.getStringExtra("messagefrombroadcastreceiver");
//Toast message here

if(message.equals("1")){
//set imagebutton1
}else if(message.equals("2")){
//set imagebutton2
}
.
.
.
.

the app was able to toast the message but it force closes when i added the if-else-if portion. i really have no idea what i'm doing wrong

LOGCATs are...

01-24 18:39:35.271: W/dalvikvm(30941): threadid=1: thread exiting with uncaught exception (group=0x400f1560)
01-24 18:39:35.341: E/AndroidRuntime(30941): FATAL EXCEPTION: main
01-24 18:39:35.341: E/AndroidRuntime(30941): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thesistrial1/com.example.thesistrial1.Thesis}: java.lang.NullPointerException
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.os.Looper.loop(Looper.java:130)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.main(ActivityThread.java:3735)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at java.lang.reflect.Method.invokeNative(Native Method)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at java.lang.reflect.Method.invoke(Method.java:507)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at dalvik.system.NativeStart.main(Native Method)
01-24 18:39:35.341: E/AndroidRuntime(30941): Caused by: java.lang.NullPointerException
01-24 18:39:35.341: E/AndroidRuntime(30941):    at com.example.thesistrial1.Thesis.onCreate(Thesis.java:63)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-24 18:39:35.341: E/AndroidRuntime(30941):    ... 11 more
01-24 18:39:35.341: E/AndroidRuntime(30941): [Blue Error Handler] Make Debugging Report file for main
01-24 18:39:35.341: E/AndroidRuntime(30941): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thesistrial1/com.example.thesistrial1.Thesis}: java.lang.NullPointerException
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.os.Looper.loop(Looper.java:130)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.main(ActivityThread.java:3735)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at java.lang.reflect.Method.invokeNative(Native Method)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at java.lang.reflect.Method.invoke(Method.java:507)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at dalvik.system.NativeStart.main(Native Method)
01-24 18:39:35.341: E/AndroidRuntime(30941): Caused by: java.lang.NullPointerException
01-24 18:39:35.341: E/AndroidRuntime(30941):    at com.example.thesistrial1.Thesis.onCreate(Thesis.java:63)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 18:39:35.341: E/AndroidRuntime(30941):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-24 18:39:35.341: E/AndroidRuntime(30941):    ... 11 more

Caused by: java.lang.NullPointerException 01-24 18:39:35.341: E/AndroidRuntime(30941): at com.example.thesistrial1.Thesis.onCreate(Thesis.java:63)

Row 63...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM