简体   繁体   English

无法解析方法“getdrawable()”

[英]Cannot resolve method 'getdrawable()

I'm a newbie in programming.我是编程的新手。 I want to build a QR Code Generator that the QR Code can be saved or downloaded.我想构建一个可以保存或下载二维码的二维码生成器。

Here's my code for the generator:这是我的生成器代码:

    public class GeneratorActivity extends AppCompatActivity {

    EditText text;
    Button gen_btn;
    ImageView image;
    String text2Qr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generator);
        text = findViewById(R.id.text);
        gen_btn = findViewById(R.id.gen_btn);
        image = findViewById(R.id.image);
        gen_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text2Qr = text.getText().toString().trim();
                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                try{
                    BitMatrix bitMatrix = multiFormatWriter.encode(text2Qr, BarcodeFormat.QR_CODE,200,200);
                    BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                    Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                    image.setImageBitmap(bitmap);
                    bitmap = ((BitmapDrawable) ImageView.getdrawable()).getBitmap();

                }
                catch (WriterException e){
                    e.printStackTrace();
                }



            }
        });
    }
}

I got a error that I cannot resolve method 'getdrawable()我收到一个错误,我无法解析方法“getdrawable()”

anybody know how to fix this?有人知道如何解决这个问题吗?

Here's the screenshot of the error: screenshot这是错误的截图:截图

getDrawable

Return a drawable object associated with a particular resource ID and styled for the specified theme.返回与特定资源 ID 关联并为指定主题设置样式的可绘制对象。

You should pass OBJECT你应该通过OBJECT

bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();

FYI供参考

Drawable drawable = image.getDrawable();

You already have a bitmap and you have set it to imageview.您已经有一个位图,并且已将其设置为 imageview。 Why do you need this line为什么需要这条线

bitmap = ((BitmapDrawable) ImageView.getdrawable()).getBitmap();位图 = ((BitmapDrawable) ImageView.getdrawable()).getBitmap();

The error is saying that there is no method called getDrawable() in the ImageView class.错误是说 ImageView 类中没有名为 getDrawable() 的方法。

get the drawable like from imageview like从imageview中获取drawable like

Drawable myDrawable = imageView.getDrawable();

You can compare it with a drawable resource like您可以将其与可绘制资源进行比较,例如

if(iv.getDrawable()==getResources().getDrawable(R.drawable.image1)){
    //do work here
}

I think you can use it Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);我想你可以用它Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix); directly.直接地。 may be not need bitmap = ((BitmapDrawable) image.getdrawable()).getBitmap();可能不需要bitmap = ((BitmapDrawable) image.getdrawable()).getBitmap();

You are getting drawable from empty imageview that will return null.您可以从将返回 null 的空imageviewdrawable for getting bitmap from drawble , try this method.要从drawble获取位图,请尝试此方法。

public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

Hope it will help you!!希望能帮到你!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM