【问题标题】:Number format exception in inflating xml for fragment为片段膨胀 xml 中的数字格式异常
【发布时间】:2013-01-01 12:25:56
【问题描述】:

早上好, 我有以下片段:

public class NewIntervPhotoFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    if(container == null) {
        return null;
    }
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.newintervphoto, container, false);
}
//rest of code goes here 

xml的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText 
    android:id="@+id/newintervPhotoDescription"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/newintervPhotoDescription"
    android:inputType="textCapSentences|textMultiLine"
    android:lines="10"
    android:gravity="left|top"/>

 <Button 
   android:id="@+id/newintervPhotoItem"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:background="@android:color/transparent"
   android:text="@string/TakePhoto"
   android:textStyle="@style/TextStyle"
   android:layout_below="@id/newintervPhotoDescription"
   android:layout_centerHorizontal="true"/>

</RelativeLayout> 

在styles.xml中:

<style 
name="TextStyle">
  <item name="android:textSize">20sp</item> 
  <item name="android:textColor">#FFFFFF</item>
</style> 

上面的样式用在同一个项目的其他xml文件中没有问题。 在这种形式中,我得到了致命的异常(logcat 的一部分):

01-01 11:49:32.635: E/AndroidRuntime(780): FATAL EXCEPTION: main
01-01 11:49:32.635: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start   activity ComponentInfo{cz.triinfo.terminal/cz.triinfo.terminal.ui.NewIntervPhotoActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class <unknown>
01-01 11:49:32.635: E/AndroidRuntime(780): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class <unknown>
01-01 11:49:32.635: E/AndroidRuntime(780): Caused by: java.lang.reflect.InvocationTargetException
01-01 11:49:32.635: E/AndroidRuntime(780): Caused by: java.lang.NumberFormatException: unable to parse '@2131165192' as integer

现在,当我将 android:textStyle="@style/TextStyle" 替换为 android:textSize=20sp android:textColor=FFFFFF 时,异常消失了。 我的问题是:在第一种情况下我做错了什么,即带有android:textStyle 的 xml,更一般地说,是否可以在 Fragments 中使用样式?

提前致谢! styles.xml 放在 res/values 中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style 
    name="TextStyle">
    <item name="android:textSize">20sp</item> 
    <item name="android:textColor">#FFFFFF</item>
  </style>
</resources> 

所以问题真的在别处。

【问题讨论】:

    标签: android exception android-fragments android-xml


    【解决方案1】:

    您需要使用 android:textAppearance 而不是 android:textStyle 来应用样式。

    【讨论】:

    • 请点赞这个,因为这是正确答案!谢谢!我有同样的问题...
    【解决方案2】:

    检查您的 styles.XML 如何与 android 开发网站上的格式匹配:here。你可能错过了resources 标签。

    [编辑]:刚刚阅读了您的第一条评论,并认为这里值得一提(如果您没有查看我之前提到的链接):

    <resources>
        Required
    This must be the root node. No attributes.
    

    添加这个会让你的styles.xml看起来像:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <style name="TextStyle">
        <item name="android:textSize">20sp</item> 
        <item name="android:textColor">#FFFFFF</item>
      </style>
    </resources>
    

    尝试一下然后说这不是一个真正的解决方案怎么样?

    【讨论】:

    • 感谢您的回答。我在同一个项目的其他 xml 文件中使用提到的样式没有问题。问题出在其他地方。
    猜你喜欢
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多