有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓操作系统与摄像头(Java)

我试图编写我的第一个应用程序(手电筒),但该程序没有在模拟器上运行,也没有在手机上运行,无法启动方法。open(),程序启动。 我将非常感谢您的帮助。)

AndroidManifest。xml

<uses-permission 安卓:name="安卓.permission.CAMERA" />
<uses-permission 安卓:name="安卓.permission.FLASHLIGHT" />
<uses-feature 安卓:name="安卓.hardware.camera" 安卓:required="false" />
<uses-feature 安卓:name="安卓.hardware.camera.flash" 安卓:required="false" />

main活动:

package com.example.user.flashlight;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.hardware.Camera;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ImageButton;
import 安卓.widget.TextView;

public class MainActivity extends AppCompatActivity {

Camera camera;
TextView textViewSwitch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onClick(View view)  {

    ImageButton LedSwitch;

    LedSwitch = (ImageButton) findViewById(R.id.LedSwitch);

    //Set the button's appearance
    LedSwitch.setSelected(!LedSwitch.isSelected());

    //Handle selected state change
    if(LedSwitch.isSelected()) {
        LedSwitch.setImageResource(R.drawable.led);

        textViewSwitch = (TextView) findViewById(R.id.textViewSwitch);
        textViewSwitch.setText("on");

        Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
        camera.startPreview();
    }

    //Handle de-select state change
    else {
        textViewSwitch.setText("off");
        LedSwitch.setImageResource(R.drawable.blackled);
        camera.stopPreview();
        camera.release();
    }

}
}

LOGCAT:

11-24 18:37:04.408 23517-23517/? I/art: Late-enabling -Xcheck:jni
11-24 18:37:09.466 23517-23517/com.example.user.flashlight W/System:      ClassLoader referenced unknown path: /data/app/com.example.user.flashlight-1/lib/x86
11-24 18:37:10.094 23517-23522/com.example.user.flashlight W/art: Suspending all threads took: 12.623ms
11-24 18:37:11.136 23517-23522/com.example.user.flashlight W/art: Suspending all threads took: 30.819ms
11-24 18:37:11.862 23517-23550/com.example.user.flashlight D/OpenGLRenderer:  Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-24 18:37:11.910 23517-23517/com.example.user.flashlight D/: HostConnection::get() New Host Connection established 0xaa29aab0, tid 23517
11-24 18:37:11.971 23517-23550/com.example.user.flashlight D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
11-24 18:37:11.979 23517-23550/com.example.user.flashlight D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-24 18:37:11.989 23517-23550/com.example.user.flashlight D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
11-24 18:37:12.011 23517-23550/com.example.user.flashlight D/: HostConnection::get() New Host Connection established 0xaed12b30, tid 23550
11-24 18:37:12.035 23517-23550/com.example.user.flashlight I/OpenGLRenderer: Initialized EGL, version 1.4
11-24 18:37:12.134 23517-23550/com.example.user.flashlight W/EGL_emulation: eglSurfaceAttrib not implemented
11-24 18:37:12.134 23517-23550/com.example.user.flashlight W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaed13580, error=EGL_SUCCESS
11-24 18:37:13.573 23517-23517/com.example.user.flashlight I/Choreographer: Skipped 55 frames!  The application may be doing too much work on its main thread.
11-24 18:37:34.167 23517-23517/com.example.user.flashlight W/CameraBase: An error occurred while connecting to camera: 0
11-24 18:37:34.170 23517-23517/com.example.user.flashlight D/AndroidRuntime: Shutting down VM
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime: FATAL EXCEPTION: main
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime: Process: com.example.user.flashlight, PID: 23517
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method for 安卓:onClick
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.view.View.performClick(View.java:5198)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.view.View$PerformClick.run(View.java:21147)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Handler.handleCallback(Handler.java:739)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Handler.dispatchMessage(Handler.java:95)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Looper.loop(Looper.java:148)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.view.View.performClick(View.java:5198) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.view.View$PerformClick.run(View.java:21147) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Handler.handleCallback(Handler.java:739) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Handler.dispatchMessage(Handler.java:95) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Looper.loop(Looper.java:148) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.app.ActivityThread.main(ActivityThread.java:5417) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:  Caused by: java.lang.RuntimeException: Fail to connect to camera service
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.hardware.Camera.<init>(Camera.java:495)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.hardware.Camera.open(Camera.java:356)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.example.user.flashlight.MainActivity.onClick(MainActivity.java:41)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.view.View.performClick(View.java:5198) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.view.View$PerformClick.run(View.java:21147) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Handler.handleCallback(Handler.java:739) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Handler.dispatchMessage(Handler.java:95) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.os.Looper.loop(Looper.java:148) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at 安卓.app.ActivityThread.main(ActivityThread.java:5417) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)

共 (1) 个答案

  1. # 1 楼答案

    你错过了变量赋值:

    camera = Camera.open();
    

    下次发布你的stacktrace