2

我想在我的 Android 应用程序项目中包含一个资源 xml 文件,以保存一些旨在具有特定用途的字符串值categories_names.xml,将其复制到res/values文件夹中,但是一旦我尝试运行应用程序,它就会退出并抛出InflateException. 奇怪的是,甚至无法从启动器活动中访问此资源文件。这怎么可能?我在下面报告回溯。

07-13 13:24:21.842: W/dalvikvm(27215): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-13 13:24:21.872: E/AndroidRuntime(27215): FATAL EXCEPTION: main
07-13 13:24:21.872: E/AndroidRuntime(27215): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mikidep.amount/com.mikidep.amount.MainActivity}: android.view.InflateException: Binary XML file line #53: Error inflating class android.widget.Button
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.os.Looper.loop(Looper.java:137)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.ActivityThread.main(ActivityThread.java:5041)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at java.lang.reflect.Method.invokeNative(Native Method)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at java.lang.reflect.Method.invoke(Method.java:511)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at dalvik.system.NativeStart.main(Native Method)
07-13 13:24:21.872: E/AndroidRuntime(27215): Caused by: android.view.InflateException: Binary XML file line #53: Error inflating class android.widget.Button
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at com.mikidep.amount.NumberFragment.onCreateView(NumberFragment.java:36)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.Fragment.performCreateView(Fragment.java:1695)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.Activity.onCreateView(Activity.java:4717)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.Activity.setContentView(Activity.java:1881)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at com.mikidep.amount.MainActivity.onCreate(MainActivity.java:29)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.Activity.performCreate(Activity.java:5104)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-13 13:24:21.872: E/AndroidRuntime(27215):    ... 11 more
07-13 13:24:21.872: E/AndroidRuntime(27215): Caused by: java.lang.reflect.InvocationTargetException
07-13 13:24:21.872: E/AndroidRuntime(27215):    at java.lang.reflect.Constructor.constructNative(Native Method)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
07-13 13:24:21.872: E/AndroidRuntime(27215):    ... 34 more
07-13 13:24:21.872: E/AndroidRuntime(27215): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=54; index=68
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.StringBlock.get(StringBlock.java:64)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:720)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.TypedArray.getString(TypedArray.java:124)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.widget.TextView.<init>(TextView.java:928)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.widget.Button.<init>(Button.java:107)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.widget.Button.<init>(Button.java:103)
07-13 13:24:21.872: E/AndroidRuntime(27215):    ... 37 more

编辑:这是提到的布局。我还必须说我已经尝试删除/替换似乎导致问题的元素,但它只会应用于另一个元素。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F2F2F2"
    android:orientation="vertical"
    tools:context=".NumberActivity" >

    <TextView
        android:id="@+id/displayTextView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fontFamily="Lato"
        android:gravity="center"
        android:text="0"
        android:textColor="#F07B7B"
        android:textSize="70dp"
        android:typeface="normal"
        tools:ignore="SpUsage" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/clear_next_background" >

        <ImageView
            android:id="@+id/clearCrossImageView"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/clear_number"
            android:paddingRight="0dp"
            android:src="@drawable/clear_cross_png" />

        <ImageView
            android:id="@+id/nextArrowImageView"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/clearCrossImageView"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/next"
            android:cropToPadding="false"
            android:paddingLeft="0dp"
            android:src="@drawable/next_arrow_png" />

        <Button
            android:id="@+id/clearButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/clearCrossImageView"
            android:background="@drawable/clear_next_background"
            android:text="@string/clear_number"
            android:textAllCaps="true"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/nextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/nextArrowImageView"
            android:background="@drawable/clear_next_background"
            android:text="@string/next"
            android:textAllCaps="true"
            android:textColor="#ffffff" />

    </RelativeLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="7"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button8"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="8"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button9"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="9"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="4"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="5"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="6"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="1"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="2"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="3"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="6" >

            <ImageButton
                android:id="@+id/delImageButton"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="2"
                android:background="@drawable/number_button"
                android:contentDescription="@string/delete_button_description"
                android:cropToPadding="true"
                android:padding="17dp"
                android:scaleType="fitCenter"
                android:src="@drawable/del_button_png" />

            <Button
                android:id="@+id/button0"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="@drawable/number_button"
                android:text="0"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/buttonComma"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="@string/comma"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/buttonPlusMinus"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/number_button"
                android:text="+/-"
                android:textColor="#ffffff"
                android:textSize="25dp"
                tools:ignore="HardcodedText" />

        </TableRow>
    </TableLayout>

</LinearLayout>

编辑 2:我发现将这些字符串资源与其他字符串资源放在 res/values/strings.xml 文件中具有相同的效果,所以我猜这些资源存在一些问题。我在下面列出它们。

<string name="category_angles">Angles</string>
<string name="category_area">Area</string>
<string name="category_electricity">Electricity</string>
<string name="category_kitchen">Kitchen</string>
<string name="category_data">Data</string>
<string name="category_energy">Energy</string>
<string name="category_force">Force</string>
<string name="category_frequency">Frequency</string>
<string name="category_illumination">Illumination</string>
<string name="category_luminance">Luminance</string>
<string name="category_lenght">Lenght</string>
<string name="category_mass">Mass</string>
<string name="category_power">Power</string>
<string name="category_pressure">Pressure</string>
<string name="category_temperature">Temperature</string>
<string name="category_time">Time</string>
<string name="category_typography">Typography</string>
<string name="category_torsion">Torsion</string>
<string name="category_currencies">Currencies</string>
<string name="category_speed">Speed</string>
<string name="category_internet_speed">Internet speed</string>
<string name="category_volume">Volume</string>

编辑 3:我检查了我的 R.java 生成的文件以确保它不是资源生成器问题,但它看起来不错。粘贴下面的字符串部分。

public static final class string {
    public static final int action_settings=0x7f060017;
    public static final int animations_duration=0x7f060022;
    public static final int app_name=0x7f060016;
    public static final int categories_fragment_name=0x7f060021;
    public static final int category_angles=0x7f060000;
    public static final int category_area=0x7f060001;
    public static final int category_currencies=0x7f060012;
    public static final int category_data=0x7f060004;
    public static final int category_electricity=0x7f060002;
    public static final int category_energy=0x7f060005;
    public static final int category_force=0x7f060006;
    public static final int category_frequency=0x7f060007;
    public static final int category_illumination=0x7f060008;
    public static final int category_internet_speed=0x7f060014;
    public static final int category_kitchen=0x7f060003;
    public static final int category_lenght=0x7f06000a;
    public static final int category_luminance=0x7f060009;
    public static final int category_mass=0x7f06000b;
    public static final int category_power=0x7f06000c;
    public static final int category_pressure=0x7f06000d;
    public static final int category_speed=0x7f060013;
    public static final int category_temperature=0x7f06000e;
    public static final int category_time=0x7f06000f;
    public static final int category_torsion=0x7f060011;
    public static final int category_typography=0x7f060010;
    public static final int category_volume=0x7f060015;
    public static final int clear_number=0x7f060019;
    public static final int comma=0x7f06001d;
    public static final int delete_button_description=0x7f06001b;
    public static final int display_placeholder=0x7f06001c;
    public static final int hello_world=0x7f060020;
    public static final int history=0x7f060023;
    public static final int main_activity_name=0x7f06001e;
    public static final int next=0x7f06001a;
    public static final int number_fragment_name=0x7f060018;
    public static final int title_activity_category_list=0x7f06001f;
}
4

6 回答 6

2

我知道这是一个迟到的回应,但我遇到了同样的问题,删除我的“android:textAllCaps”样式解决了这个问题。我不知道为什么..仍在调查。

于 2014-10-13T20:39:56.410 回答
2

你可以尝试一些事情(如果你还没有)看看你是否取得了进展:

  1. 在布局中使用硬编码字符串,而不是从 strings.xml 加载它们。

  2. 快速了解 ArrayIndexOutOfBoundsException 的由来

07-13 13:24:21.872: E/AndroidRuntime(27215): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=54; index=68
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.StringBlock.get(StringBlock.java:64)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:720)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.TypedArray.getString(TypedArray.java:124)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.widget.TextView.<init>(TextView.java:928)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.widget.Button.<init>(Button.java:107)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.widget.Button.<init>(Button.java:103)
07-13 13:24:21.872: E/AndroidRuntime(27215):    .. 37 more

似乎是与设置字体系列有关的这一行的 TextView init。

从您的布局 xml 中,您似乎正在使用自定义字体系列(“Lato”)。

<TextView
    android:id="@+id/displayTextView"
    ...
    android:fontFamily="Lato"
    ...
/>

您的应用程序支持吗?如果我错了,请纠正我,但据我所知,默认 Roboto 字体的系统支持的字体系列(自 API 16 起)是“sans-serif”、“sans-serif-light”、“sans-serif-浓缩”或“无衬线薄”。

java.lang.reflect.InvocationTargetException

07-13 13:24:21.872: E/AndroidRuntime(27215): Caused by: java.lang.reflect.InvocationTargetException
07-13 13:24:21.872: E/AndroidRuntime(27215):    at java.lang.reflect.Constructor.constructNative(Native Method)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
07-13 13:24:21.872: E/AndroidRuntime(27215):    ... 34 more

当系统无法通过反射找到并实例化您的自定义字体时,可能会“可能”导致后续的InflateException.

因此,可能值得尝试先使用默认系统字体,看看是否能解决问题。

于 2013-07-31T04:54:19.013 回答
1

您的问题在于检索字符串值。线索在这里...

E/AndroidRuntime(27215): 引起: java.lang.ArrayIndexOutOfBoundsException: length=54; index=68 07-13 13:24:21.872: E/AndroidRuntime(27215): 在 android.content.res.StringBlock.get(StringBlock.java:64) 07-13 13:24:21.872: E/AndroidRuntime(27215 ): 在 android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458) 07-13 13:24:21.872: E/AndroidRuntime(27215): 在 android.content.res.TypedArray.loadStringValueAt(TypedArray. java:720) 07-13 13:24:21.872: E/AndroidRuntime(27215): 在 android.content.res.TypedArray.getString(TypedArray.java:124) 07-13 13:24:21.872: E/AndroidRuntime( 27215): 在 android.widget.TextView.(TextView.java:928)

看起来它正在尝试检索一个不存在的字符串值,或者它认为不存在。看看字符串文件中是否有任何可能导致它无法正确解析的内容。

从你提供的内容来看,我不能比这更具体了。

于 2013-07-25T09:49:04.000 回答
1

尝试删除您的 bin 和 gen 文件夹,然后刷新您的项目。

于 2013-07-13T13:46:01.417 回答
1

它显示 ArrayIndexOutOfBound 异常。

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=54; index=68
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.StringBlock.get(StringBlock.java:64)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:720)
07-13 13:24:21.872: E/AndroidRuntime(27215):    at android.content.res.TypedArray.getString(TypedArray.java:124)

这意味着它是由过度索引 Array 而不是它的大小引起的。所以可能是您使用了高于数组大小的资源索引。所以首先检查一下,然后尝试运行。

于 2013-07-31T04:25:57.487 回答
1

您是否要为 Button 充气?因为这似乎行不通,这可能是获得例外的一个原因。如果你是,这就是方法:

LinearLayout view = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.yada_layout, null);
Button b = (Button) view.findViewById(R.id.clearButton);
于 2013-07-30T20:21:14.867 回答