首先,我是整个Android派对的初学者。我试图编写代码来点击按钮开始和停止相机预览。我最终想要从预览中提取单个帧,但我遇到了使预览正确启动的问题。
以下是我的代码。我的相机预览是在点击launchFrames
按钮时开始/停止。每次按钮告诉onClickListener
是否调用camera_start()
或camera_stop()
方法时,切换为true / false的布尔值。目前,我的应用程序在创建按钮后(以及onClickListener
实现之前)崩溃。
我通过添加日志消息确定了崩溃的位置,如附加的LogCat
所示。请详细说明导致此错误的原因以及您可能会注意到我的代码(一般结构或特定事物)的任何其他问题。如果我未能提供有关我的问题的足够信息,请告诉我。
public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback{
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public static final int REQUEST_VIDEO_CAPTURE = 1;
public static final String TAG = "MAIN";
private static boolean toggleFrameLauncher = false;
Button launchFrames;
Camera mcamera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Started onCreate method"); ////////// DEBUG ///////////
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "setContentView executed successfully"); /////// DEBUG //////
Button launchFrames = (Button)findViewById(R.id.button_frames);
Log.i(TAG, "Created button reference successfully");//// DEBUG ////////
launchFrames.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
if (!toggleFrameLauncher) {
toggleFrameLauncher = !toggleFrameLauncher;
start_camera();
} else {
toggleFrameLauncher = !toggleFrameLauncher;
stop_camera();
}
}
});
Log.i(TAG, "Button configured correctly"); /DEBUG-Code does not reach here/
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void start_camera() {
try {
mcamera = Camera.open();
} catch(RuntimeException e) {
Log.e(TAG, "init_camera: " + e);
return;
}
Camera.Parameters param;
param = mcamera.getParameters();
//modify parameter
//param.setPreviewFrameRate(20);
param.setPreviewSize(480,320);
param.setPreviewFormat(ImageFormat.RGB_565);
mcamera.setParameters(param);
try {
mcamera.setPreviewDisplay(surfaceHolder);
mcamera.startPreview();
} catch (Exception e) {
Log.e(TAG, "init_camera: " + e);
return;
}
}
private void stop_camera() {
mcamera.stopPreview();
mcamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
if(previewing) {
mcamera.stopPreview();
previewing = false;
}
if (mcamera != null) {
try {
mcamera.setPreviewDisplay(surfaceHolder);
mcamera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mcamera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mcamera.stopPreview();
mcamera.release();
mcamera = null;
previewing = false;
}
}
这是我的LogCat:
05-24 18:58:33.463: I/MAIN(19366): Started onCreate method
05-24 18:58:33.503: I/MAIN(19366): setContentView executed successfully
05-24 18:58:33.503: I/MAIN(19366): Created button reference successfully
05-24 18:58:33.503: W/dalvikvm(19366): threadid=1: thread exiting with uncaught exception (group=0x4153aba8)
05-24 18:58:33.503: E/AndroidRuntime(19366): FATAL EXCEPTION: main
05-24 18:58:33.503: E/AndroidRuntime(19366): Process: com.example.myfirstapp, PID: 19366
05-24 18:58:33.503: E/AndroidRuntime(19366): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.os.Handler.dispatchMessage(Handler.java:102)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.os.Looper.loop(Looper.java:136)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-24 18:58:33.503: E/AndroidRuntime(19366): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 18:58:33.503: E/AndroidRuntime(19366): at java.lang.reflect.Method.invoke(Method.java:515)
05-24 18:58:33.503: E/AndroidRuntime(19366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-24 18:58:33.503: E/AndroidRuntime(19366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-24 18:58:33.503: E/AndroidRuntime(19366): at dalvik.system.NativeStart.main(Native Method)
05-24 18:58:33.503: E/AndroidRuntime(19366): Caused by: java.lang.NullPointerException
05-24 18:58:33.503: E/AndroidRuntime(19366): at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:46)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.Activity.performCreate(Activity.java:5231)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-24 18:58:33.503: E/AndroidRuntime(19366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-24 18:58:33.503: E/AndroidRuntime(19366): ... 11 more
这是我活动的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "horizontal">
<Button
android:id="@+id/button_frames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_box_1"
android:layout_toRightOf = "@id/button_video"
android:text="@string/button_frames" />
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
答案 0 :(得分:0)
根据您的日志消息,launchFrames
必须为null
。通过注意堆栈跟踪告诉您崩溃的确切行号,您可以更直接地告诉它。
尝试清理项目(例如,项目&gt;在Eclipse主菜单中清理),看看是否有帮助。