将background xml加载到textview时,Android崩溃了

时间:2014-09-01 16:58:33

标签: android xml textview

在我的Android应用程序中,我尝试将此xml加载到textview背景中。但它崩溃了

有谁知道什么是错的以及如何解决它?

由于

protected TextView createCountView(Context context, int text, int bg_id) {
    TextView nametv = new TextView(context);
    nametv.setText(String.valueOf(text));
    nametv.setTextAppearance(context, R.style.count);
    nametv.setBackgroundResource(bg_id);
    LinearLayout.LayoutParams textparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textparams.gravity = Gravity.CENTER;
    textparams.setMargins(20, 0, 0, 0);
    nametv.setLayoutParams(textparams);
    return nametv;
}

protected TextView createUnAppImageCountView(Context context) {
    return createCountView(context, numberOfUnapprovedImages(), R.drawable.imagecount_bg);
}

imagecount_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<solid xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#076300"/> 

</solid>

错误

09-01 12:55:24.295: E/AndroidRuntime(9193): FATAL EXCEPTION: main
09-01 12:55:24.295: E/AndroidRuntime(9193): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.arin/android.arin.NavigationScreen}: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/imagecount_bg.xml from drawable resource ID #0x7f020039
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.os.Looper.loop(Looper.java:137)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.ActivityThread.main(ActivityThread.java:4950)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at dalvik.system.NativeStart.main(Native Method)
09-01 12:55:24.295: E/AndroidRuntime(9193): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/imagecount_bg.xml from drawable resource ID #0x7f020039
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.content.res.Resources.loadDrawable(Resources.java:1951)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.content.res.Resources.getDrawable(Resources.java:672)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.view.View.setBackgroundResource(View.java:14453)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.Fish.createCountView(Fish.java:204)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.Fish.createUnAppImageCountView(Fish.java:193)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.Category.addCountViews(Category.java:386)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.Fish.createNodeWrapper(Fish.java:161)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.Category.<init>(Category.java:52)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.TreeMaker.getCategory(TreeMaker.java:53)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.TreeMaker.getRoot(TreeMaker.java:43)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at fish.TreeMaker.getCategoryRoot(TreeMaker.java:33)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.arin.NavigationScreen.setUpCarousel(NavigationScreen.java:156)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.arin.NavigationScreen.onCreate(NavigationScreen.java:54)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.Activity.performCreate(Activity.java:5177)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
09-01 12:55:24.295: E/AndroidRuntime(9193):     ... 11 more
09-01 12:55:24.295: E/AndroidRuntime(9193): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: invalid drawable tag solid
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
09-01 12:55:24.295: E/AndroidRuntime(9193):     at android.content.res.Resources.loadDrawable(Resources.java:1948)
09-01 12:55:24.295: E/AndroidRuntime(9193):     ... 26 more

1 个答案:

答案 0 :(得分:0)

检查答案here

根据链接中的答案,按以下方式更改imagecount_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#076300"/> 
</shape>

您应将<solid>括在<shape>内,而不是在<solid>