1

我正在尝试background动态更改按钮的。该按钮已经在main.xml. 我努力了:

featuredimage.setBackgroundDrawable(res.getDrawable(R.drawable.black_gradient));

但得到以下错误:

09-03 23:16:12.279: E/AndroidRuntime(562): FATAL EXCEPTION: main
09-03 23:16:12.279: E/AndroidRuntime(562): java.lang.NullPointerException
09-03 23:16:12.279: E/AndroidRuntime(562):  at My.Taste.App.MyTasteActivity.changeCatagory(MyTasteActivity.java:188)
09-03 23:16:12.279: E/AndroidRuntime(562):  at My.Taste.App.MyTasteActivity.access$0(MyTasteActivity.java:160)
09-03 23:16:12.279: E/AndroidRuntime(562):  at My.Taste.App.MyTasteActivity$8.onClick(MyTasteActivity.java:155)
09-03 23:16:12.279: E/AndroidRuntime(562):  at android.view.View.performClick(View.java:3511)
09-03 23:16:12.279: E/AndroidRuntime(562):  at android.view.View$PerformClick.run(View.java:14105)
09-03 23:16:12.279: E/AndroidRuntime(562):  at android.os.Handler.handleCallback(Handler.java:605)
09-03 23:16:12.279: E/AndroidRuntime(562):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 23:16:12.279: E/AndroidRuntime(562):  at android.os.Looper.loop(Looper.java:137)
09-03 23:16:12.279: E/AndroidRuntime(562):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-03 23:16:12.279: E/AndroidRuntime(562):  at java.lang.reflect.Method.invokeNative(Native Method)
09-03 23:16:12.279: E/AndroidRuntime(562):  at java.lang.reflect.Method.invoke(Method.java:511)
09-03 23:16:12.279: E/AndroidRuntime(562):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-03 23:16:12.279: E/AndroidRuntime(562):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-03 23:16:12.279: E/AndroidRuntime(562):  at dalvik.system.NativeStart.main(Native Method)

是的,black_gradient 文件存在。

附加代码:

featuredimage = (ImageView) findViewById(R.id.featuredimage);
featuredinfo = (TextView) findViewById(R.id.featuredinfo);
featuredinfo.setText(Html.fromHtml("<center><big><b>FEATURED " + catagory + "</b></big></center><br>" + getFeatured().getMediumString()));
4

1 回答 1

1

应该是你的有问题res。尝试这个 :

featuredimage.setBackgroundDrawable(getResources().getDrawable(R.drawable.black_gradient));

您还可以使用android.view.View.setBackgroundResource

featuresimage.setBackgroundResource(R.drawable.black_gradient);

于 2012-09-03T23:26:31.340 回答