Android应用程序使用NullPointerException在ImageView.setImageResource上崩溃

时间:2014-12-01 19:39:13

标签: java android nullpointerexception

我一直在努力编写一个应用程序,当它开始说话时会张开嘴,并在它完成时闭嘴。我的文本到语音引擎工作正常,但当我添加代码来更改图像时,我的应用程序崩溃了。我将所有图像都放在res / drawable中,并且我确保我的图像的ID是正面的。导致问题的原因是什么?

Java代码:

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

3 个答案:

答案 0 :(得分:1)

切斯克这段代码。

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

如果您仍有问题,请添加评论。

答案 1 :(得分:0)

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

此行应使用布局中图像视图的ID,而不是drawable。它应该是这样的:

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

答案 2 :(得分:0)

face或face2为null。检查你是否有脸和脸2可画,并检查它们是否没有损坏。