Question

I want to set a button style programatically. This means without needing to define an xml file. I thought that by setting the background resource of the button with the predefined style IDs I could do this.

The line of code that fails is:

b1.setBackgroundResource(android.R.style.Widget_DeviceDefault_Light_Button_Borderless_Small);

There is an exception that states

android.content.res.Resources$NotFoundException: Resource ID #0x1030179.

What am I doing wrong?

EDIT:

I have tried to this:

Button b = new Button(context,null,android.R.style.Widget_DeviceDefault_Button_Borderless);

This no longer throws an exception but no real style is applied to the button. There is just text and there is no different states for when its pressed.

EDIT2:

Adding the original stack trace as requested:

03-03 08:17:58.810    5990-5990/legen.dary E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{legen.dary/legen.dary.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x103016c
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x103016c
            at android.content.res.Resources.getValue(Resources.java:1013)
            at android.content.res.Resources.getDrawable(Resources.java:658)
            at android.view.View.setBackgroundResource(View.java:14179)
            at legen.dary.TitleButtonView.<init>(TitleButtonView.java:24)
            at legen.dary.MainActivity.onCreate(MainActivity.java:131)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top