button.performclick() is not working

2019-09-21 06:20发布

I have a simple code which records video on clicking a button. Manual click works fine but myButton.performClick() does not work (the app stops). I have tried setPressed() too but the click by it does not cause any effect. If I directly start mediarecorder without any button click, then also the app crashes. I am clueless.

     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");
         }
}};

My log file-

  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 more

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-21 07:04

what you are trying to do is as soon as it loads you want to start recording. why do you want to automate a button click for that put the code which is inside onclick to a method and in place of myButton.performClick(); call that method

查看更多
登录 后发表回答