使用相机(Java)

时间:2015-11-24 23:03:35

标签: java android android-studio

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

的AndroidManifest.xml

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

MainActivity:

package com.example.user.flashlight;
import android.support.v7.app.AppCompatActivity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.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 android:onClick
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.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 android.view.View.performClick(View.java:5198)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.android.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 android.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 android.view.View.performClick(View.java:5198) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.android.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 android.hardware.Camera.<init>(Camera.java:495)
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.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 android.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 android.view.View.performClick(View.java:5198) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-24 18:37:34.175 23517-23517/com.example.user.flashlight E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

你错过了变量赋值:

camera = Camera.open();

下次发布您的堆栈跟踪!