拍照使应用程序崩溃

时间:2018-04-26 12:40:01

标签: android android-camera

我在android studio上开发了一个Android应用程序。 我使用的是LG G5,一切正常。我刚刚搬到三星s8,当我想通过应用程序打开相机时,它崩溃了。 我试着调试这个,我发现该应用程序崩溃: looper.java在函数循环()中。 怎么修好?

这是错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.imperialsoupgmail.tesseractexample, PID: 26176
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:6891)
                  at android.widget.TextView.performClick(TextView.java:12651)
                  at android.view.View$PerformClick.run(View.java:26083)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6938)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:6891) 
                  at android.widget.TextView.performClick(TextView.java:12651) 
                  at android.view.View$PerformClick.run(View.java:26083) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6938) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
               Caused by: java.lang.NullPointerException: file
                  at android.net.Uri.fromFile(Uri.java:453)
                  at com.imperialsoupgmail.tesseractexample.MainActivity.takePicture(MainActivity.java:258)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:6891) 
                  at android.widget.TextView.performClick(TextView.java:12651) 
                  at android.view.View$PerformClick.run(View.java:26083) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6938) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

1 个答案:

答案 0 :(得分:0)

以下示例您可以尝试,它在某种程度上对我有用。这是一个非常古老的代码。

List<String> supportedFocusModes = cameraParameters.getSupportedFocusModes();

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
           supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
} else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
} else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_MACRO)) {
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
} else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
    // auto focus on request only
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
mCamera.setParameters(cameraParameters);