我想在我的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;
}
答案 0 :(得分:2)
你可以尝试一些事情(如果你还没有),看看你是否取得了一些进展:
在布局中使用硬编码字符串,而不是从strings.xml中加载它们。
快速浏览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
似乎是此line的TextView init,它与设置字体系列有关。
从您的布局xml,您似乎使用的是自定义字体系列(“Lato”)。
<TextView
android:id="@+id/displayTextView"
...
android:fontFamily="Lato"
...
/>
您的申请是否支持此功能?纠正我,如果我错了,但从我上次所知,系统支持的字体系列(自API 16)为默认的Roboto字体是'sans-serif','sans-serif-light','sans-serif-浓缩'或'sans-serif-thin'。
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
当系统无法通过反射找到并instantiate自定义字体时,可能会“导致”InflateException
。
因此,首先尝试使用默认系统字体以确定是否可以解决问题。
答案 1 :(得分:2)
我知道这是一个迟到的回复,但我有同样的问题,并删除我的&#34; android:textAllCaps&#34;样式修复了问题。我不确定为什么,但仍在调查。
答案 2 :(得分:1)
尝试删除bin和gen文件夹,然后刷新项目。
答案 3 :(得分:1)
您的问题在于检索字符串值。线索就在这里......
E / AndroidRuntime(27215):引起:java.lang.ArrayIndexOutOfBoundsException:length = 54;指数= 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)
看起来它正在尝试检索不存在的字符串值,或者它认为不存在。看一下,看看字符串文件中是否有任何可能导致它无法正确解析的内容。
从你提供的内容来看,我不能更具体。
答案 4 :(得分:1)
你想试试按钮吗?因为it seems不起作用,这是获得异常的可能原因。如果你是,那就是这样:
LinearLayout view = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.yada_layout, null);
Button b = (Button) view.findViewById(R.id.clearButton);
答案 5 :(得分: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)
这意味着它是由过度索引数组而不是其大小引起的。所以可能你使用的资源索引高于你的数组大小。所以先检查一下,然后再尝试运行。