1

I am getting these errors in logcat:

FATAL EXCEPTION: main  Process: com.sreekanth.gifapp, PID: 14761
java.lang.IllegalStateException: Could not execute method for android:onClick

I tried changing the name of the method, but didn't work out. Here is my code:

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/btngif"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="133dp"
        android:layout_marginStart="133dp"
        android:layout_marginTop="37dp"
        android:text="capture gif"
        android:onClick="vMethod"/>
</RelativeLayout>

Activity:

public class MainActivity extends Activity {

      private   final  int VIDEO_REQUEST_CODE =100;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        public void vMethod (View view) {
            Intent camera_intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            File video_fileq = getFilepath();
            Uri video_uri = Uri.fromFile(video_fileq);
            camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,video_uri);
            Intent intent = camera_intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            startActivityForResult(camera_intent,VIDEO_REQUEST_CODE);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if(requestCode == VIDEO_REQUEST_CODE) {
                if(requestCode == RESULT_OK) {
                    Toast.makeText(getApplicationContext(),"video captured successufully",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),"video capture failed...",Toast.LENGTH_SHORT).show();
                }
            }
        }

        public File getFilepath() {
            File folder = new File("sdcard/video_captured");

            if (!folder.exists()) {
                folder.mkdir();
            }

            File video_file = new File(folder,"sample_video.mp4");
            return video_file;
        }
    }

here is the full logcat of this app.Please go throug the logcat that I have provided.These are the errors that i am facing.I tried to change the method name,but it didn't work.

  FATAL EXCEPTION: main
                                                                          Process: com.sreekanth.gifapp, PID: 23875
                                                                          java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                              at android.view.View$DeclaredOnClickListener.onClick(View.java:4697)
                                                                              at android.view.View.performClick(View.java:5609)
                                                                              at android.view.View$PerformClick.run(View.java:22263)
                                                                              at android.os.Handler.handleCallback(Handler.java:751)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at android.view.View$DeclaredOnClickListener.onClick(View.java:4692)
                                                                              at android.view.View.performClick(View.java:5609) 
                                                                              at android.view.View$PerformClick.run(View.java:22263) 
                                                                              at android.os.Handler.handleCallback(Handler.java:751) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                                                                           Caused by: android.os.FileUriExposedException: file:///sdcard/video_captured/sample_video.mp4 exposed beyond app through ClipData.Item.getUri()
                                                                              at android.os.StrictMode.onFileUriExposed(StrictMode.java:1826)
                                                                              at android.net.Uri.checkFileUriExposed(Uri.java:2346)
                                                                              at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
                                                                              at android.content.Intent.prepareToLeaveProcess(Intent.java:8964)
                                                                              at android.content.Intent.prepareToLeaveProcess(Intent.java:8949)
                                                                              at android.app.Instrumentation.execStartActivity(Instrumentation.java:1519)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4265)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4224)
                                                                              at com.sreekanth.gifapp.MainActivity.vMethod(MainActivity.java:37)
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at android.view.View$DeclaredOnClickListener.onClick(View.java:4692) 
                                                                              at android.view.View.performClick(View.java:5609) 
                                                                              at android.view.View$PerformClick.run(View.java:22263) 
                                                                              at android.os.Handler.handleCallback(Handler.java:751) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

2 Answers2

0

Why not try the other way...

Declare a button object:

Button button_gif;

get your Button resource from xml in onCreate

 button_gif = (Button) findViewById(R.id.btngif);

and implement the onClickListener interface in your Activity, so you've to override the onClick method:

@Override
public void onClick(View v) {
//Do needful on button press
}
ruben
  • 1,758
  • 5
  • 25
  • 47
  • I'm sorry but this answer is not right and doesn't help to solve problem – Dima Kozhevin Oct 05 '17 at 18:19
  • You can use 2 abilities to set OnClickListener: 1) as suggested in this answer(in code) 2) as you did in your question(in XML). It is not a mistake in your case. – Dima Kozhevin Oct 05 '17 at 20:04
  • @DimaKozhevin Yes using xml is not working so I thought other method should just work fine – ruben Oct 05 '17 at 21:26
  • @ShrikantSurendran please check if your android development environment is installed properly, clean and rebuild the project etc – ruben Oct 05 '17 at 21:27
0

Does your app have permissions to use Camera and Storage? (manifest, ask user... - https://developer.android.com/training/permissions/requesting.html?hl=es-419)