我想以编程方式设置按钮样式。这意味着无需定义xml文件。我认为通过使用预定义的样式ID设置按钮的后台资源,我可以这样做。
失败的代码行是:
b1.setBackgroundResource(android.R.style.Widget_DeviceDefault_Light_Button_Borderless_Small);
状态
有一个例外android.content.res.Resources$NotFoundException: Resource ID #0x1030179.
我做错了什么?
编辑:
我试过这个:
Button b = new Button(context,null,android.R.style.Widget_DeviceDefault_Button_Borderless);
这不再抛出异常,但按钮没有应用真正的样式。只有文字,按下它时没有不同的状态。
EDIT2:
按要求添加原始堆栈跟踪:
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)