E / AndroidRuntime:致命例外:main java.lang.IllegalStateException:无法执行android:onClick

时间:2018-06-23 20:33:00

标签: java android

仅创建一个按钮时,一切运行良好,但是每当我为不同的按钮创建一种方法时,就会出现这些错误:

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method for android:onClick   Caused by: java.lang.reflect.InvocationTargetException   Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0

请帮助。

java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      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:866)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5610) 
                      at android.view.View$PerformClick.run(View.java:22265) 
                      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:866) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
                   Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
                      at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
                      at android.content.res.ResourcesImpl.openRawResourceFd(ResourcesImpl.java:287)
                      at android.content.res.Resources.openRawResourceFd(Resources.java:1263)
                      at android.media.MediaPlayer.create(MediaPlayer.java:931)
                      at android.media.MediaPlayer.create(MediaPlayer.java:914)
                      at com.example.chakshu.griddemo.MainActivity.pressed(MainActivity.java:26)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5610) 
                      at android.view.View$PerformClick.run(View.java:22265) 
                      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:866) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

这是我的activity_main.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"
   android:id="@+id/activity_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.chakshu.griddemo.MainActivity">

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:text="A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:onClick="pressed"/>
    <Button
    android:text="B"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:layout_column="1"
    android:layout_row="0"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
        android:onClick="pressed"/>
    <Button
        android:text="C"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_column="0"
        android:layout_row="1"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:onClick="pressed"/>
    <Button
        android:text="D"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_column="1"
        android:layout_row="1"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:onClick="pressed"/>
    <Button
        android:text="e"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:layout_column="0"
        android:layout_row="2"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:onClick="pressed"/>

    <Button
        android:text="f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_column="1"
        android:layout_row="2"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:onClick="pressed"/>

    <Button
        android:text="g"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button6"
        android:layout_column="0"
         android:layout_row="3"
       android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:onClick="pressed"/>
    <Button
        android:text="h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button7"
        android:layout_column="1"
        android:layout_row="3"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:onClick="pressed"/>
       </GridLayout>
     </RelativeLayout>

这是我的MainActivity.java代码:

 package com.example.chakshu.griddemo;

 import android.media.MediaPlayer;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;

  public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
   public void pressed(View view) {

    int id = view.getId();
    String ourId = "";

    ourId = view.getResources().getResourceEntryName(id);

    int resourceId = getResources().getIdentifier(ourId, "raw", 
    "com.example.chakshu.griddemo");

    MediaPlayer mplayer = MediaPlayer.create(this, resourceId);
    mplayer.start();


    }

2 个答案:

答案 0 :(得分:0)

我认为真正的错误在中间,android.content.res.Resources$NotFoundException: Resource ID #0x0 at行表明它找不到您传递的资源。

您传递的资源通常类似于R.raw.my_resource_name

更多信息herehere

答案 1 :(得分:0)

问题在于在以下代码中找不到有效的资源ID:

public void pressed(View view) {

    int id = view.getId();
    String ourId = "";

    ourId = view.getResources().getResourceEntryName(id);

    int resourceId = getResources().getIdentifier(ourId, "raw", 
    "com.example.chakshu.griddemo");

    ...
}

由于{id}不存在,getResourceEntryName()将抛出Resources.NotFoundException的位置,如果找不到有效的资源ID,getIdentifier将返回0。

因此,将您的点击侦听器方法更新为以下内容:

public void pressed(View view) {

    int id = view.getId();

    try {
      String ourId = getResources().getResourceEntryName(id);
      int resId = getResources().getIdentifier(ourId, "raw", getPackageName());

      if(resId == 0) {
        Log.d("MainActivity", "error, no found raw");
      } else {
        MediaPlayer mplayer = MediaPlayer.create(this, resourceId);
        mplayer.start();
      }
    } catch(NotFoundException e) {
      Log.d("MainActivity", "error, no found resource = " + e.toString);
    }


}

而是根据按钮ID名称,尝试使用android:tag[getTag][3]方法。例如,使用以下按钮:

<Button
    android:text="A"
    android:tag="raw_resource_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_gravity="fill"
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:onClick="pressed"/>

然后,您可以使用以下代码:

public void pressed(View view) {

  String resName = (String) view.getTag();
  int resId = getResources().getIdentifier(resName, "raw", getPackageName());

  if(resId == 0) {
     Log.d("MainActivity", "error, no found raw");
  } else {
     MediaPlayer mplayer = MediaPlayer.create(this, resourceId);
     mplayer.start();
  }
}