我有一个简单的代码,可以在单击按钮时录制视频。手动点击工作正常,但myButton.performClick()
不起作用(应用程序停止)。我也尝试过 setPressed() 但它的点击不会产生任何影响。如果我直接启动 mediarecorder 而不点击任何按钮,那么应用程序也会崩溃。我一无所知。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
setContentView(R.layout.main);
//Get Camera for preview
myCamera = getCameraInstance();
if(myCamera == null){
Toast.makeText(AndroidVideoCapture.this,
"Fail to get Camera",
Toast.LENGTH_LONG).show();
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
myButton.performClick();
}
Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(recording){
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
//Exit after saved
finish();
}else{
//Release Camera before MediaRecorder start
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(AndroidVideoCapture.this,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}};
我的日志文件-
05-16 12:40:35.680: E/MediaRecorderJNI(1710): Application lost the surface
05-16 12:40:35.680: E/AndroidRuntime(1710): in writeCrashedAppName, pkgName :com.exercise.AndroidVideoCapture
05-16 12:40:35.680: E/AndroidRuntime(1710): FATAL EXCEPTION: main
05-16 12:40:35.680: E/AndroidRuntime(1710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exercise.AndroidVideoCapture/com.exercise.AndroidVideoCapture.AndroidVideoCapture}: java.lang.NullPointerException
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.os.Looper.loop(Looper.java:130)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-16 12:40:35.680: E/AndroidRuntime(1710): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:40:35.680: E/AndroidRuntime(1710): at java.lang.reflect.Method.invoke(Method.java:507)
05-16 12:40:35.680: E/AndroidRuntime(1710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
05-16 12:40:35.680: E/AndroidRuntime(1710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
05-16 12:40:35.680: E/AndroidRuntime(1710): at dalvik.system.NativeStart.main(Native Method)
05-16 12:40:35.680: E/AndroidRuntime(1710): Caused by: java.lang.NullPointerException
05-16 12:40:35.680: E/AndroidRuntime(1710): at com.exercise.AndroidVideoCapture.AndroidVideoCapture$1.onClick(AndroidVideoCapture.java:145)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.view.View.performClick(View.java:2485)
05-16 12:40:35.680: E/AndroidRuntime(1710): at com.exercise.AndroidVideoCapture.AndroidVideoCapture.onCreate(AndroidVideoCapture.java:74)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-16 12:40:35.680: E/AndroidRuntime(1710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-1
6 12:40:35.680: E/AndroidRuntime(1710): ... 11 更多