Android getIdentifier returns 0x1? -
on small set of devices running application users getting crash when changing image via preferences. full stack trace:
java.lang.runtimeexception: unable resume activity {com.printandpixel.lolhistory/com.printandpixel.lolhistory.activities.mainactivity}: android.content.res.resources$notfoundexception: resource id #0x1 @ android.app.activitythread.performresumeactivity(activitythread.java:3346) @ android.app.activitythread.handleresumeactivity(activitythread.java:3377) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2728) @ android.app.activitythread.access$900(activitythread.java:172) @ android.app.activitythread$h.handlemessage(activitythread.java:1422) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:145) @ android.app.activitythread.main(activitythread.java:5832) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194) caused by: android.content.res.resources$notfoundexception: resource id #0x1 @ android.content.res.resources.getvalue(resources.java:2329) @ android.content.res.resources.getdrawable(resources.java:1846) @ android.content.res.resources.getdrawable(resources.java:1814) @ com.printandpixel.lolhistory.util.generalutils.getdrawablefromname(generalutils.java:226) @ com.printandpixel.lolhistory.util.generalutils.gethomepageheader(generalutils.java:277) @ com.printandpixel.lolhistory.activities.mainactivity.onresume(mainactivity.java:252) @ android.app.instrumentation.callactivityonresume(instrumentation.java:1255) @ android.app.activity.performresume(activity.java:6338) @ android.app.activitythread.performresumeactivity(activitythread.java:3335) ... 11 more
the line in function getdrawablefromname being referred here following:
return context.getresources().getdrawable(resourceid);
the full implementation of method follows:
public static drawable getdrawablefromname(string name, context context) { int resourceid = context.getresources().getidentifier(name, "drawable", context.getpackagename()); if (resourceid > 0) { return context.getresources().getdrawable(resourceid); } else { tracker t = ((lolhistory) ((activity) context).getapplication()).gettracker(lolhistory.trackername.app_tracker); t.send(new hitbuilders.eventbuilder().setcategory("event").setaction("drawablenotfound").setlabel(name).build()); return null; } }
from can tell far happening on android 5 , not on android 5 devices. far can tell documentation specifies getidentifier
should return 0 if drawable isn't found in case returns 0x1? there hundreds of different images in application , user reports have 0x1 invalid id.
is safe assume resourcesids of 1 invalid?
edit: per @ρяσѕρєяk suggestion, handle deprecation of getdrawable(id), , fail gracefully when resources unexpectedly not found have edited function follows:
public static drawable getdrawablefromname(string name, context context) { int resourceid = context.getresources().getidentifier(name, "drawable", context.getpackagename()); drawable drawable = null; try { if (build.version.sdk_int >= build.version_codes.lollipop) { drawable = context.getresources().getdrawable(resourceid, context.gettheme()); } else { drawable = context.getresources().getdrawable(resourceid); } } catch (resources.notfoundexception e) { tracker t = ((lolhistory) ((activity) context).getapplication()).gettracker(lolhistory.trackername.app_tracker); t.send(new hitbuilders.eventbuilder().setcategory("event").setaction("drawablenotfound").setlabel(name).build()); } return drawable; }
the issue ended not being due getdrawablefromname
function. unrelated :(. had function in app specific default preference value follows:
public static drawable gethomepageheader(context context) { sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(context); string headerimagename = prefs.getstring(context.getstring(r.string.pref_champion_header_name), "darius_600"); drawable drawable = getdrawablefromname(headerimagename, context); if (drawable == null) { drawable = new colordrawable(contextcompat.getcolor(context, r.color.green)); } return drawable; }
this preference set in preferenceactivity
following listpreference
:
<listpreference android:key="pref_champion_header" android:entries="@array/headers" android:entryvalues="@array/header_values" android:summary="champion splash appear @ top of homepage" android:title="homepage champion" android:defaultvalue="1"/>
with following 2 arrays being referenced:
<string-array name="headers" translatable="false"> <item>darius</item> <item>garen</item> <item>malphite</item> <item>gnar</item> <item>buccaneer tristana</item> </string-array> <string-array name="header_values" translatable="false"> <item>darius_600</item> <item>garen_600</item> <item>malphite_600</item> <item>gnar_600</item> <item>buccaneer_tristana_600</item> </string-array>
for whatever reason on devices defaultvalue="1"
resolved first entry of header_values
, on others resolved "1"
resulted "1"
being passed custom getdrawablefromname
method. fix changed defaultvalue
explicitly equal first value of header_values array aka "darius_600"
Comments
Post a Comment