我正在使用SurfaceView.
制作手电筒应用程序我的代码不会产生错误,但在运行时会停止。我不知道为什么会停止。
这是完整的代码:
MainActivity.java
package com.example.flashlight;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private final static String TAG = "Flashlight Test";
Camera cam;
ToggleButton togglebtn;
Parameters p;
private SurfaceView surface;
private SurfaceHolder surfaceholder;
private boolean hasFlash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
// if Device doesn't support flashlight
if (!hasFlash) {
AlertDialog.Builder noflash = new AlertDialog.Builder(this);
noflash.setTitle(getString(R.string.errord_title));
noflash.setMessage(getString(R.string.errord_content));
noflash.setPositiveButton(getString(R.string.errord_posbutton), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
finish();
}
});
noflash.show();
}
togglebtn = (ToggleButton)findViewById(R.id.togglebtn_switch);
togglebtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
if (cam == null) {
cam = Camera.open();
}
p = cam.getParameters();
List<String> flashModes = p.getSupportedFlashModes();
if (isChecked) {
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
} else {
}
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
} else {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
}
cam.setParameters(p);
cam.startPreview();
} catch (Exception e) {
Log.d(TAG, "Exception" + e);
Toast.makeText(MainActivity.this, "Flashlight: Exception", Toast.LENGTH_SHORT).show();
e.printStackTrace();
if (cam != null) {
cam.stopPreview();
cam.release();
}
}
}
});
surface = (SurfaceView) this.findViewById(R.id.surface);
surfaceholder = surface.getHolder();
surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceholder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cam.setPreviewDisplay(holder);
} catch (IOException e) {
Log.e(TAG, "IOException setPreviewDisplay()", e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
这是Logcat(e)
03-21 01:09:24.861: E/AndroidRuntime(17649): FATAL EXCEPTION: main
03-21 01:09:24.861: E/AndroidRuntime(17649): Process: com.example.flashlight, PID: 17649
03-21 01:09:24.861: E/AndroidRuntime(17649): java.lang.NullPointerException
03-21 01:09:24.861: E/AndroidRuntime(17649): at com.example.flashlight.MainActivity.surfaceCreated(MainActivity.java:102)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.SurfaceView.updateWindow(SurfaceView.java:601)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.SurfaceView.access$000(SurfaceView.java:94)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:879)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2131)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1234)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6469)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.Choreographer.doFrame(Choreographer.java:573)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.os.Handler.handleCallback(Handler.java:733)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.os.Handler.dispatchMessage(Handler.java:95)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.os.Looper.loop(Looper.java:157)
03-21 01:09:24.861: E/AndroidRuntime(17649): at android.app.ActivityThread.main(ActivityThread.java:5356)
03-21 01:09:24.861: E/AndroidRuntime(17649): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 01:09:24.861: E/AndroidRuntime(17649): at java.lang.reflect.Method.invoke(Method.java:515)
03-21 01:09:24.861: E/AndroidRuntime(17649): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-21 01:09:24.861: E/AndroidRuntime(17649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-21 01:09:24.861: E/AndroidRuntime(17649): at dalvik.system.NativeStart.main(Native Method)
我该如何解决这个问题?请帮忙。 :(
答案 0 :(得分:0)
您的cam
变量指向null
,因为您没有创建任何实例。
Camera cam = new Camera();
希望有所帮助
答案 1 :(得分:0)
在调用开始预览之前设置“surface holder.add call back(this)”。例如....
SurfaceView prev = //find your surfaceview I'd
SurfaceHolder holder = prev.get holder();
holder.addCallback(this);
mCamera.setPreviewDisplay(holder);//surround with try/catch
//get your patameters
//set your parameters
//set flashmode
//THEN start preview!