猿问

E / AndroidRuntime:致命例外:main java.lang.Illegal

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

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

请帮助。

这是我的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();



    }


呼如林
浏览 395回答 2
2回答

饮歌长啸

我认为真正的错误在中间,这行android.content.res.Resources$NotFoundException: Resource ID #0x0 at提示它找不到您传入的资源。您传递的资源通常类似于 R.raw.my_resource_name

ABOUTYOU

问题是在以下代码中找不到有效的资源ID:public void pressed(View view) {&nbsp; &nbsp; int id = view.getId();&nbsp; &nbsp; String ourId = "";&nbsp; &nbsp; ourId = view.getResources().getResourceEntryName(id);&nbsp; &nbsp; int resourceId = getResources().getIdentifier(ourId, "raw",&nbsp;&nbsp; &nbsp; "com.example.chakshu.griddemo");&nbsp; &nbsp; ...}由于该ID不存在,将在其中抛出getResourceEntryName(),如果找不到有效的资源ID Resources.NotFoundException,则getIdentifier将返回0。因此,将您的点击监听器方法更新为以下内容:public void pressed(View view) {&nbsp; &nbsp; int id = view.getId();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; String ourId = getResources().getResourceEntryName(id);&nbsp; &nbsp; &nbsp; int resId = getResources().getIdentifier(ourId, "raw", getPackageName());&nbsp; &nbsp; &nbsp; if(resId == 0) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("MainActivity", "error, no found raw");&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; MediaPlayer mplayer = MediaPlayer.create(this, resourceId);&nbsp; &nbsp; &nbsp; &nbsp; mplayer.start();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch(NotFoundException e) {&nbsp; &nbsp; &nbsp; Log.d("MainActivity", "error, no found resource = " + e.toString);&nbsp; &nbsp; }}而是根据按钮ID名称,尝试使用android:tag和[getTag][3]方法。例如,使用以下按钮:<Button&nbsp; &nbsp; android:text="A"&nbsp; &nbsp; android:tag="raw_resource_name"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:id="@+id/button"&nbsp; &nbsp; android:layout_column="0"&nbsp; &nbsp; android:layout_row="0"&nbsp; &nbsp; android:layout_gravity="fill"&nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; android:layout_rowWeight="1"&nbsp; &nbsp; android:onClick="pressed"/>然后,您可以使用以下代码:public void pressed(View view) {&nbsp; String resName = (String) view.getTag();&nbsp; int resId = getResources().getIdentifier(resName, "raw", getPackageName());&nbsp; if(resId == 0) {&nbsp; &nbsp; &nbsp;Log.d("MainActivity", "error, no found raw");&nbsp; } else {&nbsp; &nbsp; &nbsp;MediaPlayer mplayer = MediaPlayer.create(this, resourceId);&nbsp; &nbsp; &nbsp;mplayer.start();&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答