相机预览的问题
根据书上的一个实例做了预览的功能,但是在虚拟机上点开以后就直接闪退,可以通过编译,不知道哪儿出了问题。初来乍到,还想请帮忙看一下,我用的是4.1的SDKHello.java
package com.example.camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
public class Hello extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new CameraPreview(this));
}
}
CameraPreview.java
package com.example.camera;
import java.io.IOException;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.ErrorCallback;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class CameraPreview extends Su易做图ceView implements Su易做图ceHolder.Callback{
protected Context context;
private Su易做图ceHolder holder;
protected Camera camera;
CameraPreview(Context context){
super(context);
this.context=context;
holder=getHolder();
holder.addCallback(this);
holder.setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void su易做图ceCreated(Su易做图ceHolder holder){
Log.d("TEST", "su易做图ceCreated");
if(camera==null){
try{
camera=Camera.open();
}catch(RuntimeException e){
((Activity)context).finish();
Toast.makeText(context, e.getMessage(),Toast.LENGTH_LONG).show();
}
}
if(camera!=null){
camera.setPreviewCallback(new PreviewCallback(){
public void onPreviewFrame(byte[] data,Camera camera){
Log.d("TEST", "onPreviewFrame: preview: data="+data);
}
});
camera.setOneShotPreviewCallback(new PreviewCallback(){
public void onPreviewFrame(byte[] data,Camera camera){
Log.d("TEST", "onPreviewFrame:short preview: data="+data);
}
});
camera.setErrorCallback(new ErrorCallback(){
public void onError(int error,Camera camera){
Log.d("TEST","onError:error="+error);
}
}
);
}try{
camera.setPreviewDisplay(holder);
}catch(IOException e){
camera.release();
camera=null;
((Activity)context).finish();
Toast.makeText(context, e.getMessage(),Toast.LENGTH_LONG).show();
}
}
public void su易做图ceChanged(Su易做图ceHolder holder,int format,int width,int height){
Log.d("TEST", "su易做图ceChanged");
if(camera==null){
((Activity)context).finish();
}else{
camera.stopPreview();
setPictureFormat(format);
setPreviewSize(width,height);
camera.startPreview();
}
}
public void su易做图ceDestroyed(Su易做图ceHolder holder){
Log.d("TEST", "su易做图ceDestroyed");
if(camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
}
protected void setPictureFormat(int format){
try{
Camera.Parameters params=camera.getParameters();
List<Integer> supported=params.getSupportedPictureFormats();
if(supported !=null){
for(int f: supported){
if(f==format){
params.setPreviewFormat(format);
camera.setParameters(params);
break;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
protected void setPreviewSize(int width,int height){
Camera.Parameters params=camera.getParameters();
List<Camera.Size>supported=params.getSupportedPreviewSizes();
if(supported!=null){
for(Camera.Size size:supported){
if(size.width<=width&&size.height<=height){
params.setPreviewSize(size.width,size.height);
camera.setParameters(params);
break;
}
}
}
}
}
--------------------编程问答-------------------- 发个log,同时去真机上看一下 --------------------编程问答-------------------- log是logcat里的内容吗?
08-08 01:33:10.535: D/TEST(678): su易做图ceCreated
08-08 01:33:10.753: D/AndroidRuntime(678): Shutting down VM
08-08 01:33:10.753: W/dalvikvm(678): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-08 01:33:10.983: E/AndroidRuntime(678): FATAL EXCEPTION: main
08-08 01:33:10.983: E/AndroidRuntime(678): java.lang.NullPointerException
08-08 01:33:10.983: E/AndroidRuntime(678): at com.example.camera.CameraPreview.su易做图ceCreated(CameraPreview.java:70)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.Su易做图ceView.updateWindow(Su易做图ceView.java:543)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.Su易做图ceView.access$000(Su易做图ceView.java:81)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.Su易做图ceView$3.onPreDraw(Su易做图ceView.java:169)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.Choreographer.doFrame(Choreographer.java:525)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.os.Handler.handleCallback(Handler.java:615)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.os.Looper.loop(Looper.java:137)
08-08 01:33:10.983: E/AndroidRuntime(678): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-08 01:33:10.983: E/AndroidRuntime(678): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 01:33:10.983: E/AndroidRuntime(678): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 01:33:10.983: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-08 01:33:10.983: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 01:33:10.983: E/AndroidRuntime(678): at dalvik.system.NativeStart.main(Native Method)
08-08 01:33:15.353: I/Process(678): Sending signal. PID: 678 SIG: 9
--------------------编程问答-------------------- 08-08 01:33:10.983: E/AndroidRuntime(678): java.lang.NullPointerException
08-08 01:33:10.983: E/AndroidRuntime(678): at com.example.camera.CameraPreview.su易做图ceCreated(CameraPreview.java:70)
CameraPreview.java:70,这边空指针。
以后遇到这种问题,可以先看一下log,看看有什么exception --------------------编程问答-------------------- 怎么找出来是哪一行的变量出现问题呢 --------------------编程问答-------------------- 请问这个问题你怎么解决的啊,我现在也碰到这个问题了 --------------------编程问答-------------------- Camera 一般都要真机测试!~
补充:移动开发 , Android