我有 .png 图像,我res/drawable-hdpi/delete.png
想将它作为图标添加到我的菜单item
中。
我用了:
删除.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/deletebutton">
</bitmap>
菜单activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/create"
android:showAsAction="always"
android:title="@string/createTask"/>
<item
android:id="@+id/delete"
android:showAsAction="always"
android:icon="@drawable/delete"
android:title="@string/deleteTask" />
</menu>
但它不起作用。
我删除了xml文件并尝试直接加载图像这里是日志文件。
04-02 17:46:50.887: E/AndroidRuntime(11957): FATAL EXCEPTION: main
04-02 17:46:50.887: E/AndroidRuntime(11957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.devicecontrolpanel/com.example.devicecontrolpanel.Main}: android.content.res.Resources$NotFoundException: Resource ID #0x7f020008
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.ActivityThread.access$600(ActivityThread.java:139)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.os.Looper.loop(Looper.java:156)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.ActivityThread.main(ActivityThread.java:4987)
04-02 17:46:50.887: E/AndroidRuntime(11957): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 17:46:50.887: E/AndroidRuntime(11957): at java.lang.reflect.Method.invoke(Method.java:511)
04-02 17:46:50.887: E/AndroidRuntime(11957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-02 17:46:50.887: E/AndroidRuntime(11957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-02 17:46:50.887: E/AndroidRuntime(11957): at dalvik.system.NativeStart.main(Native Method)
04-02 17:46:50.887: E/AndroidRuntime(11957): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020008
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.content.res.Resources.getValue(Resources.java:1105)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.content.res.Resources.getDrawable(Resources.java:674)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.widget.CompoundButton.setButtonDrawable(CompoundButton.java:189)
04-02 17:46:50.887: E/AndroidRuntime(11957): at com.example.devicecontrolpanel.Main.listAllAlarms(Main.java:69)
04-02 17:46:50.887: E/AndroidRuntime(11957): at com.example.devicecontrolpanel.Main.onCreate(Main.java:25)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.Activity.performCreate(Activity.java:4538)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
04-02 17:46:50.887: E/AndroidRuntime(11957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
04-02 17:46:50.887: E/AndroidRuntime(11957): ... 11 more