简体   繁体   English

在Android 5.0.1中找不到资源异常

[英]Resource not found exception In Android 5.0.1

I Am working on camera, when I launch the application, activity will be visible for the user and I have a button to call camera intent. 我正在使用相机,当我启动应用程序时,活动对用户可见,并且我有一个按钮可以调用相机意图。 I have all my images used in activity in drawable-xxhdpi folder, This will work fine and now I will click the button to capture image and click on save the application crashes by giving Resource not found exception 我将所有图像都用在了drawable-xxhdpi文件夹中的活动中,这将正常工作,现在我将单击按钮以捕获图像,然后单击“ 未找到资源”来保存应用程序崩溃

Then I copied images from drawable-xxhdpi folder to drawable folder, The application works fine, Can anyone tell me the reason why First time Images are taking from drawable-xxhdpi folder and not the second time (After clicking save in camera). 然后,我将图像从drawable-xxhdpi文件夹复制到了drawable文件夹,该应用程序运行正常,谁能告诉我第一次图像是从drawable-xxhdpi文件夹而不是第二次图像的原因(单击“保存在相机中”)。

This issue is only coming in Android 5.0.1, and works fine in Android 4.4.2 此问题仅在Android 5.0.1中出现,并在Android 4.4.2中正常运行

Any help will be appreciated 任何帮助将不胜感激

mainLayout = inflater.inflate(R.layout.inner_fragment_layout_with_header, null);
headerLayout = (RelativeLayout) mainLayout.findViewById(R.id.header);
headerLayout.setBackgroundResource(R.drawable.profile_handheld_header_bg);

crash log 崩溃日志

05-19 11:54:44.194: E/AndroidRuntime(14497):    at java.lang.reflect.Method.invoke(Method.java:372)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
05-19 11:54:44.194: E/AndroidRuntime(14497): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f02018f
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.content.res.Resources.getValue(Resources.java:2345)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.content.res.Resources.getDrawable(Resources.java:1850)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.content.Context.getDrawable(Context.java:408)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.view.View.setBackgroundResource(View.java:17212)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.cc.android.fragments.BaseFragment.processHeader(BaseFragment.java:147)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.cc.android.fragments.BaseFragment.getViewApplyingHeaderConfiguration(BaseFragment.java:127)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.cc.android.fragments.BaseFragment.onCreateView(BaseFragment.java:103)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.cc.android.fragments.ContentFragment.onCreateView(ContentFragment.java:64)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at roboguice.activity.RoboFragmentActivity.onStart(RoboFragmentActivity.java:60)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.cc.android.activities.BaseActivity.onStart(BaseActivity.java:139)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at com.android.cc.android.activities.RootPotraiteActivity.onStart(RootPotraiteActivity.java:224)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.app.Activity.performStart(Activity.java:6329)
05-19 11:54:44.194: E/AndroidRuntime(14497):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)

However you solved your issue, this happen due to device size Your Android4.4.2 device uses drawable-xxhdpi resource folder and your Android 5.0.1 devices may be smaller in size it is searching images in drawable-hdpi or other resource folder where images are not present so it troughs the exception Resource not found . 但是,您已解决了该问题,这是由于设备大小引起的,您的Android4.4.2设备使用了drawable-xxhdpi资源文件夹,而您的Android 5.0.1设备的大小可能较小,它正在drawable-hdpi或其他资源文件夹中搜索图像不存在,因此它会处理“ Resource not found ”异常。

Note: 注意:

Issue is not with android versions 问题不是Android版本

Suppose you put your images in drawable-xxhdpi and your are trying to run your application on Device or emulator which have xxhdpi type device. 假设您将图像放在drawable-xxhdpi ,并且试图在具有xxhdpi类型设备的设备或仿真器上运行应用程序。 then it will run. 然后它将运行。 and if your are try to run same application in ldpi or mdpi type device then it will crash with ResourceNotFoundException just because of unavailability of your images in drawable-ldpi , mdpi , 如果您正在尝试运行同一个应用程序ldpimdpi类型的设备,然后它会与崩溃ResourceNotFoundException只是因为在你的图像不可用的drawable-ldpi , mdpi

Punch line 妙处

If you will put your images in just drawable folder then crash will not happen because android will take it for all device regardless from device configuration or type. 如果将图像放置在drawable文件夹中,则不会发生崩溃,因为无论设备配置或类型如何,Android都会将其用于所有设备。

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

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