0

我已经更改了我的应用程序,因此用户可以对整个应用程序有 2 种不同的样式。我已经更新了我创建的自定义选项卡,因此它们链接到一个样式,该样式反过来(取决于所选样式)链接到可绘制的 Selector xml(即显示选择或未选择的内容)。

运行它会给我以下错误

01-09 10:46:28.701: E/AndroidRuntime(375): FATAL EXCEPTION: main
01-09 10:46:28.701: E/AndroidRuntime(375): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myappname/com.myappname.RoundSelect}: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.os.Looper.loop(Looper.java:123)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 10:46:28.701: E/AndroidRuntime(375):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 10:46:28.701: E/AndroidRuntime(375):  at java.lang.reflect.Method.invoke(Method.java:507)
01-09 10:46:28.701: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 10:46:28.701: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 10:46:28.701: E/AndroidRuntime(375):  at dalvik.system.NativeStart.main(Native Method)
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
01-09 10:46:28.701: E/AndroidRuntime(375):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-09 10:46:28.701: E/AndroidRuntime(375):  at com.myappname.RoundSelect.createTabView(RoundSelect.java:234)
01-09 10:46:28.701: E/AndroidRuntime(375):  at com.myappname.RoundSelect.setupTab(RoundSelect.java:223)
01-09 10:46:28.701: E/AndroidRuntime(375):  at com.myappname.RoundSelect.setupCustomTabs(RoundSelect.java:215)
01-09 10:46:28.701: E/AndroidRuntime(375):  at com.myappname.RoundSelect.onCreate(RoundSelect.java:90)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-09 10:46:28.701: E/AndroidRuntime(375):  ... 11 more
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: java.lang.reflect.InvocationTargetException
01-09 10:46:28.701: E/AndroidRuntime(375):  at java.lang.reflect.Constructor.constructNative(Native Method)
01-09 10:46:28.701: E/AndroidRuntime(375):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
01-09 10:46:28.701: E/AndroidRuntime(375):  ... 23 more
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/custom_tab_text_selector_black.xml from drawable resource ID #0x7f020023
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.content.res.Resources.loadDrawable(Resources.java:1697)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.view.View.<init>(View.java:1951)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.widget.TextView.<init>(TextView.java:344)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.widget.TextView.<init>(TextView.java:337)
01-09 10:46:28.701: E/AndroidRuntime(375):  ... 26 more
01-09 10:46:28.701: E/AndroidRuntime(375): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: <item> tag requires a 'drawable' attribute or child tag defining a drawable
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
01-09 10:46:28.701: E/AndroidRuntime(375):  at android.content.res.Resources.loadDrawable(Resources.java:1694)
01-09 10:46:28.701: E/AndroidRuntime(375):  ... 30 more

自定义背景代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    style="?custom_tab_background_selector">

    <TextView 
        android:id="@+id/customTabsText" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        style="?custom_tab_text_selector">
    </TextView>

</LinearLayout>

调用这个的方法

private static View createTabView(Context pContext, String pTabTitleText) {

        View view = LayoutInflater.from(pContext).inflate(R.layout.custom_tabs_background, null);

        TextView tv = (TextView) view.findViewById(R.id.customTabsText);

        tv.setText(pTabTitleText);

        return view;

    }

剩下的只是一个 xml 主题,它指向一个 Selector XML,后者又指向一些可绘制的 xml 文件。这是选择器 XML

<?xml version="1.0" encoding="utf-8"?>
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active tab -->
    <item 
        android:state_selected="true" 
        android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@drawable/custom_tab_background_selected_black" />
    <!--  Inactive tab -->
    <item 
        android:state_selected="false" 
        android:state_focused="false"
        android:state_pressed="false" 
        android:drawable="@drawable/custom_tab_background_unselected_black" />
    <!--  Pressed tab -->
    <item 
        android:state_pressed="true" 
        android:drawable="@android:color/transparent" />
    <!--  Selected tab (using d-pad) -->
    <item 
        android:state_focused="true" 
        android:state_selected="true"
        android:state_pressed="false" 
        android:drawable="@android:color/transparent" />
</selector>

关于我为什么会收到此错误的任何想法都会很棒。谢谢你的时间

4

1 回答 1

0

It appears I tracked it down.

the background of the tab I was using android:background=.... but was also using the same for the colour of the text and it should have been android:textColour=....

all works now. Sorry for the confusion

于 2013-01-09T11:49:18.080 回答