0

我目前正在尝试访问和使用图像来代替我正在使用的 Spinner 中的文本。如果我要专门使用文本而不是可绘制对象,则以下代码有效。

我之前引用过这个问题:Storing R.drawable IDs in XML array

希望解决我的问题,但是我无法弄清楚我哪里出错了。我似乎不断得到:

Error: No resource found that matches the given name (at '^index_1' with value '@drawable/ic_launcher.png')  

等等,对于每个图像。每个可绘制对象都存在并位于适当的位置,代码显然超出了我的理解。

<string-array name="rating_array">
        <item>N/A</item>
        <item>@drawable/ic_launcher.png</item>
        <item>@drawable/smile.png</item>
        <item>@drawable/stale.png</item>
        <item>@drawable/sad.png</item>
        <item>@drawable/angry.png</item>
    </string-array>

[编辑]

我已经摆脱了 .png 扩展名。但是,我仍然收到相同的错误。有什么额外的建议吗?

还得到:

我摆脱了这些错误,我现在得到:

\res\layout\activity_main.xml:12:错误:错误:找不到与给定名称匹配的资源(在“contentDescription”处,值为“@string/rate_main”)。\res\layout\activity_main.xml:28:错误:错误:找不到与给定名称匹配的资源(在“条目”处,值为“@array/category_array”)。\res\layout\activity_main.xml:36:错误:错误:找不到与给定名称匹配的资源(在“条目”处,值为“@array/rating_array”)。res\layout\activity_main.xml:46:错误:错误:找不到与给定名称匹配的资源(在“条目”处,值为“@array/descriptor_array”)。res\menu\main.xml:3:错误:错误:找不到与给定名称匹配的资源(在“标题”处,值为“@string/action_settings”)。

但是,作为错误,当我删除这些列表文件时,我没有问题。此外,由于某种原因,它说它正在影响我的其他数组,但是“rating_array”是唯一不是您的标准文本列表的数组。

与此相关的完整代码:

字符串.xml

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

    <string-array name="rating_array">
        <item>N/A</item>

    </string-array>

    <string-array name="descriptor_array">
        <item>N/A</item>
        <item>Price</item>
        <item>Service</item>
    </string-array>

    <string-array name="category_array">
        <item>N/A</item>
        <item>Restaurants</item>
        <item>Hotels</item>
        <item>Theater</item>
        <item>Shopping</item>
    </string-array>



    <string name="app_name">W!NK</string>
    <string name="action_settings">Settings</string>



    <string name="rate_main">Rate</string>


</resources>

MainActivity.Java

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

public class MainActivity extends Activity {

    private Spinner spinbutton1, spinbutton2, spinbutton3;





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




    }

    public void addListenerOnSpinnerItemSelection(){
        spinbutton1 = (Spinner) findViewById(R.id.spinbutton1);
            spinbutton1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        spinbutton2 = (Spinner) findViewById(R.id.spinbutton2);
            spinbutton2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        spinbutton3 = (Spinner) findViewById(R.id.spinbutton3);
            spinbutton3.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

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




}
4

1 回答 1

1

as in error :

Error: No resource found that matches the given name (at '^index_1' with value '@drawable/ic_launcher.png')

because you are currently passing drawable image extension(.png) with drawable names in string-array. so you will need to remove file extensions(.png) for drawable names which you are using in string-arraychange it as:

<string-array name="rating_array">
        <item>N/A</item>
        <item>@drawable/ic_launcher</item>
        <item>@drawable/smile</item>
        <item>@drawable/stale</item>
        <item>@drawable/sad</item>
        <item>@drawable/angry</item>
    </string-array>
于 2013-05-06T01:37:47.477 回答