Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为drawable设置字符串值_Java_Android_String_Drawable - Fatal编程技术网

Java 为drawable设置字符串值

Java 为drawable设置字符串值,java,android,string,drawable,Java,Android,String,Drawable,我怎样才能替换这个 Drawable drawable = getResources().getDrawable(R.drawable.imagesample); 使用字符串img的值?(字符串img的值是res drawable中图像的名称) 我的代码: String img = "sampleimage"; // code for image in pop-up dialog LayoutInflater inf

我怎样才能替换这个

    Drawable drawable = getResources().getDrawable(R.drawable.imagesample);
使用字符串img的值?(字符串img的值是res drawable中图像的名称)

我的代码:

     String img = "sampleimage";


     // code for image in pop-up dialog

            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View subView = inflater.inflate(R.layout.dialog, null);
            final ImageView subImageView = (ImageView) subView.findViewById(R.id.image);
            Drawable drawable = getResources().getDrawable(R.drawable.imagesample);
            subImageView.setImageDrawable(drawable);

    // code for a pop-up dialog
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Congratulations");
            builder.setMessage("Thanks for helping me!");
            builder.setView(subView);
            builder.setPositiveButton("Ok", null);
            builder.show();

不可能。你想做的是根本不可能的

getDrawable
方法接受一个
int
参数。您正在尝试传递一个
字符串
。不能使用错误类型的参数调用方法


检查进一步参考。

当前代码有什么问题?我甚至不确定你想在这里做什么?我想使用变量的一个值,而不是使用图像资源@s_o_m_m_m_y_e_e_e_e_e_)的直接id。你不能以你想要的方式访问资源文件。您当前的代码是完美的,将给出完美的结果。是的。我知道它工作得很好,我只想知道是否有部分代码可以替换,以便使用字符串值作为源代码而不是资源id。是否可以以不同的方式更改代码,但输出相同,但图像的源代码是字符串值?如果可能的话,请参考。ThanksLike为字符串的值编写了一个替代代码,但输出(对话框中的图像)与我前面说过的相同,不能使用字符串。