致命异常:java.lang.IllegalStateException:无法执行android的方法:onClick

时间:2017-10-04 21:26:54

标签: android onclick fatal-error solver

我在logcat中收到这些错误:

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

我尝试更改方法的名称,但没有成功。这是我的代码:

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>

活动:

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

这是这个应用程序的完整logcat。请通过我提供的logcat。这是我面临的错误。我试图更改方法名称,但它没有工作。

  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 个答案:

答案 0 :(得分:0)

为什么不尝试另一种方式...

声明一个按钮对象:

Button button_gif;

onCreate

中的xml获取Button资源
 button_gif = (Button) findViewById(R.id.btngif);

并在Activity中实现 onClickListener 接口,因此您必须覆盖 onClick 方法:

@Override
public void onClick(View v) {
//Do needful on button press
}

答案 1 :(得分:0)

您的应用是否有权使用相机和存储空间? (清单,询问用户...... - https://developer.android.com/training/permissions/requesting.html?hl=es-419