Error No resource found that matches the given name (at 'src' with value '@drawable/button1')

ぐ巨炮叔叔 提交于 2019-12-07 05:19:44

问题


I thought the book had incorrect code. Tried at my home location and it worked. Found out that my eclipse was actually broken. Had to remove and re install eclipse.

This is my first post here so if I didn't follow some of the rules please let me know. I tried to search and found a few post with the same error but they dealt with strings. I'm following Android Apps for Absolute Beginners (2nd Edition) and on page 179 you bring button1.xml into your res/drawable. When I go in layout/activity_main.xml and input

<ImageButton 
    android:id="@+id/button_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button1"
    android:contentDescription="@string/app_name"/>

It errors stating that the android:src "error: Error: No resource found that matches the given name (at 'src' with value '@drawable/button1')."

Now there's a few things confusing me, I'm using eclipse and I have 4 drawable folders (drawable-hdpi,drawable-ldpi, drawable-mdpi, and drawable-xhdpi), I've went to New>File on each of the 4 drawable folders and imported a button1_focused.png, button1_pressed.png, and button1_normal.png with their respective resolutions. I've also put button1.xml in each of the folders.

button1.xml is as follows

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
            android:drawable="@drawable/button1_pressed" />
    <item android:state_focused="true"
            android:drawable="@drawable/button1_focused" />
    <item android:drawable="@drawable/button1_normal" />
</selector>

I tried android:background instead of android:src and that errors out as well. I see that its error'ing out stating that there is no file at src (or background). Just not sure what to put there.

Java Code package com.example.ui_designs;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

ImageButton imageButton = new ImageButton(context);
imageButton.setImageResource(R.drawable.button1);

}

Link to the screenshot: http://s7.postimage.org/3v4tu9ifv/UI_Designs.png


回答1:


I thought the book had incorrect code. Tried at my home location and it worked. Found out that my eclipse was actually broken. Had to remove and re install eclipse.

I apologize for any inconvenience I may had caused and appreciate the community for the assistance.




回答2:


According to the screenshot of your Project Explorer... you do not have the button1.xml in your drawable-hdpi folder.

If you wish to use alll of the same image sizes, density does not matter, you can make a new folder called nothing other then just drawable which will handle all screen densities.




回答3:


I had a similar problem because

1: I did not add the proper image's name after @/drawable

2: I also did not add the image in the drawable-ldpi folder.

<ImageView
    android:id="@+id/imageDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/nasalogo">
</ImageView>

In my case I needed to add the image called nasalogo in my drawable-ldpi folder

Also this is what my values/string.xml looks like

<?xml version="1.0" encoding="utf-8"?>

PCWorldRssReaderActivity

<string name="text_image_title">Decorating the Sky</string>

<string name="text_image_date">Mon, 19 Aug 2013 00:00:00 EST </string>
<string name="text_image">nasalogo</string>

<string name="text_image_description"> This is mosaic image</string>

Later




回答4:


Could you try doing this in your java code ?

ImageButton imageButton = new ImageButton(context);
imageButton.setImageResource(R.drawable.button1);



回答5:


I was hit by this while I was reverse engineering a project, "error: Error: No resource found that matches the given name (at 'background' with value '@color/default_bg')."

Resolution: Added this following attribute to "/project_/res/values/colors.xml"

<color name="default_bg">#aa000000</color>

Finally it looked liked something this:

<resources>
  <color name="black_overlay">#66000000</color>
   <color name="default_bg">#aa000000</color>
</resources>



回答6:


There is another solution to it. Copy all the image folders (with the same name) onto all the 4 drawable folders. and execute android:src="@drawable/filename" It worked for me then!




回答7:


I had this happen in AndroidStudio and it turned out that my file was named .png but was a jpeg.



来源:https://stackoverflow.com/questions/14612556/error-no-resource-found-that-matches-the-given-name-at-src-with-value-drawa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!