Yuvi
Yuvi

Reputation: 1350

Getting resource not found exception in android app

I am trying to build an app in AOSP, my development kit has HDPI density, I have double verified it with the following code snippet:

switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
    Log.d(TAG, "\n\n\n\n LDPI \n\n\n\n");
    break;
case DisplayMetrics.DENSITY_MEDIUM:
    Log.d(TAG, "\n\n\n\n MDPI \n\n\n\n");
    break;
case DisplayMetrics.DENSITY_HIGH:
    Log.d(TAG, "\n\n\n\n HDPI \n\n\n\n");
    // ...
    break;
case DisplayMetrics.DENSITY_XHIGH:
    Log.d(TAG, "\n\n\n\n XDPI \n\n\n\n");
    // ...
    break;
}

when I build my application in Android File system,and if I run it, then I am getting following error:

E/AndroidRuntime(  825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.Button
E/AndroidRuntime(  825):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(  825):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(  825):        at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(  825):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(  825):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  825):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  825):        at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(  825):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  825):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(  825):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(  825):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(  825):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  825): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.Button
E/AndroidRuntime(  825):        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
E/AndroidRuntime(  825):        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:823)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:729)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(  825):        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
E/AndroidRuntime(  825):        at android.app.Activity.setContentView(Activity.java:1835)
E/AndroidRuntime(  825):        at com.example.MainActivity.onCreate(MainActivity.java:96)
E/AndroidRuntime(  825):        at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(  825):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(  825):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime(  825):        ... 11 more
E/AndroidRuntime(  825): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(  825):        at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime(  825):        at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
E/AndroidRuntime(  825):        at android.view.LayoutInflater.createView(LayoutInflater.java:586)
E/AndroidRuntime(  825):        ... 29 more
E/AndroidRuntime(  825): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f02000d a=-1 r=0x7f02000d}
E/AndroidRuntime(  825):        at android.content.res.Resources.loadDrawable(Resources.java:1897)
E/AndroidRuntime(  825):        at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime(  825):        at android.widget.TextView.<init>(TextView.java:620)
E/AndroidRuntime(  825):        at android.widget.Button.<init>(Button.java:108)
E/AndroidRuntime(  825):        at android.widget.Button.<init>(Button.java:104)
E/AndroidRuntime(  825):        ... 32 more

My all the drawable resources are in drawable-hdpi.

But if I put drawables in all the drawable-* folder then app runs. But then UI is getting scattered.

Does anybody have idea, what the problem could. Do I have to add any Variable or flag in the Android.mk

=========edit=========
The app works fine if I build with eclipse. And run on the same device.
Therefore issue might be in building app along with AOSP.
========================

Any help would be highly appreciated.

Regards, Yuvi

Upvotes: 0

Views: 1742

Answers (2)

Yuvi
Yuvi

Reputation: 1350

So after struggling too long and googling for more then two days, I found the issue. There was no drawable-hdpi folder in apk. I have resolved the issue by adding the LOCAL_AAPT_FLAGS += -c mdpi,hdpi,xhdpi line in Android.mk file.

Those who are struggling with the same kind of problem follow these supper steps:

Check at which line it is giving error: Binary XML file line #11: Error inflating class android.widget.Button

In my xml file it was:

<Button
    android:id="@+id/btn_footer_back"
    style="@style/normalButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="60px"
    android:layout_marginRight="1dp"
    android:background="@drawable/button_state_selector"
    android:drawableLeft="@drawable/ic_back"
    android:drawablePadding="10px"
    android:text="@string/back"
    android:visibility="gone" />

So, what is the next step now, the log itself tells the story, check the line:

Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f02000d a=-1 r=0x7f02000d}

Here r=0x7f02000d is the resource id, that you can find in yourproject/gen/R.java if you search for 0x7f02000d in R.java then you will able to find which resource is creating the problem, in my case it was R.drawable.ic_back

Hope it helps other to resolve their problems.

Regards, Yuvi

Upvotes: 1

Yatin Wadhawan
Yatin Wadhawan

Reputation: 66

Your UI is scattered because your files are not matching to appropriate density or resolution while displaying on device. You should use density independent pixels in xml. And Create Drawable folder and put your files in it. And give reference of the drawable folder.

Upvotes: 0

Related Questions