简体   繁体   English

可绘制时未发现异常

[英]Not found exception while getting drawable

I am trying to get a drawable but I am getting resource not found exception.我正在尝试获取可绘制对象,但出现资源未找到异常。 The code is only one line actually but I am getting the same exception.代码实际上只有一行,但我遇到了同样的异常。

package com.example.tafy.remotebluerc;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class BluetoothActivity extends AppCompatActivity {

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

    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher_foreground);
    //BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getResources().getDrawable(R.drawable.ic_launcher_foreground);
}
}

This is the exception that I am getting:这是我得到的例外:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.tafy.remotebluerc, PID: 10573
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tafy.remotebluerc/com.example.tafy.remotebluerc.BluetoothActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f060055
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                  at android.app.ActivityThread.access$800(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                  at android.os.Handler.dispatchMessage(Handler.java:110)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:5299)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060055
                  at android.content.res.Resources.getValue(Resources.java:1133)
                  at android.content.res.Resources.getDrawable(Resources.java:708)
                  at com.example.tafy.remotebluerc.BluetoothActivity.onCreate(BluetoothActivity.java:16)
                  at android.app.Activity.performCreate(Activity.java:5264)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                  at android.os.Handler.dispatchMessage(Handler.java:110) 
                  at android.os.Looper.loop(Looper.java:193) 
                  at android.app.ActivityThread.main(ActivityThread.java:5299) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                  at dalvik.system.NativeStart.main(Native Method) 

Hope you can help me!希望你能帮我!

I was getting that exception because I was trying to get v24 drawable.我得到那个例外是因为我试图让 v24 可绘制。 I solved the problem with using simple drawables.我使用简单的drawables解决了这个问题。

I think the issue is come through inflating drawable.我认为这个问题是通过充气可绘制的。 The Exception is例外是

Resources.NotFoundException Resources.NotFoundException

But real exception is may be "Binary XML file line #20: invalid color state list tag gradient" .但真正的例外可能是"Binary XML file line #20: invalid color state list tag gradient" That is beacuse in api lover 24 not supported gradient.那是因为 api lover 24 不支持渐变。

Solution解决方案

Duplicate layout and remove gradient tag from layout-v23复制布局并从 layout-v23 中删除渐变标签

If your are using Image view use如果您使用的是图像视图,请使用

ImageView.setImageResource(R.drawable.some_ic) ImageView.setImageResource(R.drawable.some_ic)

instead of ImageView.setImageDrawable而不是ImageView.setImageDrawable

getDrawable() is deprecated, try using: getDrawable()已弃用,请尝试使用:

ContextCompat.getDrawable(this, R.drawable.ic_launcher_foreground);

And Of course, make sure your ic_launcher_foreground exists!当然,请确保您的 ic_launcher_foreground 存在!

If I'm correct getDrawable() is deprecated and you should be using one of the following如果我是正确的 getDrawable() 已弃用,您应该使用以下之一

1) ContextCompat.getDrawable(getActivity(), R.drawable.name); 1) ContextCompat.getDrawable(getActivity(), R.drawable.name);

which will give you a drawable with your activity theme这将为您提供带有活动主题的可绘制对象

2) ResourcesCompat.getDrawable(getResources(), R.drawable.name, null); 2) ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

which will give you a drawable without any theme这会给你一个没有任何主题的drawable

Also, Make sure you clean and rebuild your project if you haven't done so.另外,如果您还没有这样做,请确保您清理并重建您的项目。

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

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