Android Resource not found exception

旧街凉风 提交于 2020-01-03 04:40:07

问题


So I am not such a newbie in Programming, Java or Android developing, but I got a strange issue: I have made an application, quite advanced, and have it on market.

For now I have over 1000 installs and I have around 4 or 5 crash reports for a ResourceNotFoundException. The strangest thing is that the line it crashes on is on

setContentView(R.layout.some_custom_layout)

In code I am always referring to resourced by

someTxtView.setText(R.string.some_string)

So I am wondering if I used

mContext.getResources().getDrawable(mContext.getResources().getIdentifier("some_string", "string", "my.example.package"));

would the crash go away?


回答1:


I was facing the same issue and I fixed it by creating Layout Folder called "layout-small".

Based on resolutions I have created only 2 folders "layout-large" and "layout-medium". But few phones are having lower resolution it doesn't find proper resources as per the resolution. Android OS renders xml resources as per resolution. It goes and find the resources in required folders.

95+ % Android phones having resolution which matches "layout-normal" folder. But still there are Phones having lower resolution. Hence this issue occurred.

For more Details : http://developer.android.com/guide/practices/screens_support.html

Hope this helps your problem.




回答2:


If you are calling setContentView(R.layout.some_custom_layout) from the Activity's onCreate method, you should be good as long as your app compiles (and I assume it does).

AFAIK accessing your string resources via:

someTxtView.setText(R.string.some_string)

is not the way to go. You should be doing this:

getContext().getResources().getString(R.string.some_string)



回答3:


Except

setContentView(R.layout.some_custom_layout);

try using

setContentView(yourpackagename.R.layout.some_custom_layout);

that helped me a lot of times.




回答4:


I have one suggestion. Do you use in your layouts android secific resources, such as drawables or something, for example

android:backgroud="@android:drawable/some_android_drawable"

Maybe some vendors don't provide some resources with their firmware, so your app crashs. for example this issue




回答5:


someTxtView.setText(R.string.some_string)

There you set integer value to text. Its not correctly becouse it search a resousre on this value. You must write

someTextView.setText(getResources().getText(R.string.blabla));


来源:https://stackoverflow.com/questions/8375710/android-resource-not-found-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!