致命异常:java.lang.IllegalStateException:无法执行 android:onClick 的方法
FATAL EXCEPTION: java.lang.IllegalStateException: Could not execute method for android:onClick
我在 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>
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;
}
}
这是完整的 logcat app.Please 遍历我的 logcat provided.These 是我 facing.I 试图解决的错误改了方法名,还是不行
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)
为什么不尝试另一种方式...
声明一个按钮对象:
Button button_gif;
在 onCreate
中从 xml 获取按钮资源
button_gif = (Button) findViewById(R.id.btngif);
并在 Activity 中实现 onClickListener 接口,因此您必须覆盖 onClick 方法:
@Override
public void onClick(View v) {
//Do needful on button press
}
您的应用是否有权使用相机和存储?
(清单,询问用户... - https://developer.android.com/training/permissions/requesting.html?hl=es-419)
我在 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>
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;
}
}
这是完整的 logcat app.Please 遍历我的 logcat provided.These 是我 facing.I 试图解决的错误改了方法名,还是不行
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)
为什么不尝试另一种方式...
声明一个按钮对象:
Button button_gif;
在 onCreate
中从 xml 获取按钮资源 button_gif = (Button) findViewById(R.id.btngif);
并在 Activity 中实现 onClickListener 接口,因此您必须覆盖 onClick 方法:
@Override
public void onClick(View v) {
//Do needful on button press
}
您的应用是否有权使用相机和存储? (清单,询问用户... - https://developer.android.com/training/permissions/requesting.html?hl=es-419)