-1

I've been trying to write an app with a face that opens its mouth when it begins speaking and closes its mouth when it's finished. My text to speech engine works fine, but when I add the code to change the image, my app crashes. I have all of my images in res/drawable, and I've made sure my image's id is face. What's causing the problem?

Java code:

private void speak(String text) {
   ImageView face = (ImageView) findViewById(R.id.face);
   face.setImageResource(R.drawable.face2); //App crashes when this code is added
   jane.speak(text, TextToSpeech.QUEUE_FLUSH, null);
   while (jane.isSpeaking()) {

   }
   face.setImageResource(R.drawable.face1); //App also crashes with this code
}

Logcat:

12-01 14:44:14.351: I/TextToSpeech(18150): Sucessfully bound to com.samsung.SMT
12-01 14:44:14.381: I/TextToSpeech(18150): Connected to ComponentInfo{com.samsung.SMT/com.samsung.SMT.SamsungTTSService}
12-01 14:44:14.401: I/TextToSpeech(18150): Set up connection to ComponentInfo{com.samsung.SMT/com.samsung.SMT.SamsungTTSService}
12-01 14:44:14.421: I/Adreno-EGL(18150): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050_msm8960_refs/tags/AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050__release_AU ()
12-01 14:44:14.421: I/Adreno-EGL(18150): OpenGL ES Shader Compiler Version: 17.01.12.SPL
12-01 14:44:14.421: I/Adreno-EGL(18150): Build Date: 03/28/14 Fri
12-01 14:44:14.421: I/Adreno-EGL(18150): Local Branch: 
12-01 14:44:14.421: I/Adreno-EGL(18150): Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050
12-01 14:44:14.421: I/Adreno-EGL(18150): Local Patches: NONE
12-01 14:44:14.421: I/Adreno-EGL(18150): Reconstruct Branch: NOTHING
12-01 14:44:14.461: D/OpenGLRenderer(18150): Enabling debug mode 0
12-01 14:44:17.905: D/AndroidRuntime(18150): Shutting down VM
12-01 14:44:17.905: W/dalvikvm(18150): threadid=1: thread exiting with uncaught exception (group=0x41813da0)
12-01 14:44:17.905: E/AndroidRuntime(18150): FATAL EXCEPTION: main
12-01 14:44:17.905: E/AndroidRuntime(18150): Process: com.nerdologylabs.jane, PID: 18150
12-01 14:44:17.905: E/AndroidRuntime(18150): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.nerdologylabs.jane/com.nerdologylabs.jane.MainActivity}: java.lang.NullPointerException
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3700)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3743)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.app.ActivityThread.access$1400(ActivityThread.java:172)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.os.Looper.loop(Looper.java:146)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.app.ActivityThread.main(ActivityThread.java:5653)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at java.lang.reflect.Method.invokeNative(Native Method)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at java.lang.reflect.Method.invoke(Method.java:515)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at dalvik.system.NativeStart.main(Native Method)
12-01 14:44:17.905: E/AndroidRuntime(18150): Caused by: java.lang.NullPointerException
12-01 14:44:17.905: E/AndroidRuntime(18150):    at com.nerdologylabs.jane.MainActivity.speak(MainActivity.java:73)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at com.nerdologylabs.jane.MainActivity.onActivityResult(MainActivity.java:88)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.app.Activity.dispatchActivityResult(Activity.java:5734)
12-01 14:44:17.905: E/AndroidRuntime(18150):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3696)
12-01 14:44:17.905: E/AndroidRuntime(18150):    ... 11 more
12-01 14:44:19.327: I/Process(18150): Sending signal. PID: 18150 SIG: 9
user3499799
  • 17
  • 1
  • 5

3 Answers3

1

Chesck this code.

ImageView face = (ImageView) findViewById(R.drawable.face);
if (face != null) {
    Drawable drawable= getResources().getDrawable(R.drawable.face2);
    if (drawable != null) {
        img.setImageDrawable(drawable);
    }
}

If you still have problems, just add a comment.

Chefes
  • 1,892
  • 18
  • 17
  • I tried the code above and it still gave a null pointer exception. I have all of my images in my res/drawable folder. – user3499799 Dec 01 '14 at 19:52
  • I renamed my images and it didn't crash. However, the image did not change. – user3499799 Dec 01 '14 at 19:59
  • I edit the post so, check again please, and add a breackpoint in if (drawable != null) just to check if the code is executing. – Chefes Dec 01 '14 at 20:15
0

ImageView face = (ImageView) findViewById(R.drawable.face);

This line should use the id of your image view in the layout, not a drawable. It should be something like this:

ImageView face = (ImageView) findViewById(R.id.my_image_view);

Razz
  • 220
  • 1
  • 6
0

Either face or face2 is null. Check if you have a face and a face2 drawable, and check if they are not corrupt.

instanceof
  • 1,404
  • 23
  • 30