【问题标题】:Adding a fragment causes crash添加片段会导致崩溃
【发布时间】:2016-01-25 20:19:52
【问题描述】:

我遇到了与片段相关的问题。我有一个启动画面,它将“加载”3 秒,然后创建一个新的 Intent 并启动 MainActivity

主要活动代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

以及对应的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context="com.example.paolo.ebolatrack.MainActivity">


    <fragment
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:name="com.example.paolo.ebolatrack.NewsFragment"
       ></fragment>

</RelativeLayout>

以及片段的 xml:

<FrameLayout 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" tools:context="com.example.paolo.ebolatrack.NewsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent" android:layout_height="match_parent"
       android:text="@string/hello_blank_fragment" />

</FrameLayout>

这是我得到的错误(完整跟踪here):

10-26 12:05:05.153 2855-2855/com.example.paolo.ebolatrack E/AndroidRuntime: FATAL EXCEPTION: main
10-26 12:05:05.153 2855-2855/com.example.paolo.ebolatrack E/AndroidRuntime: Process: com.example.paolo.ebolatrack, PID: 2855
10-26 12:05:05.153 2855-2855/com.example.paolo.ebolatrack E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paolo.ebolatrack/com.example.paolo.ebolatrack.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
...

添加片段似乎会使应用程序崩溃。我正在使用 Android Studio。

【问题讨论】:

  • 你应该把相关代码贴在这里
  • 你的片段类在哪里?

标签: java android xml android-fragments


【解决方案1】:

这里有答案:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Must specify unique android:id, android:tag, or have a parent with an id for com.example.paolo.ebolatrack.NewsFragment

在布局 xml 中为您的片段添加 id:

<fragment
   android:id="@+id/some_frag"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:name="com.example.paolo.ebolatrack.NewsFragment"
   ></fragment>

这是缺少的行:

 android:id="@+id/some_frag"

【讨论】:

  • 试过了!谢谢你,伙计..我不知道为什么,但我完全错过了我脑海中的那条线
  • 我还有一个问题:什么意思? 标签允许布局文件在运行时动态包含不同的布局。在布局编辑时,要使用的特定布局是未知的。您可以在编辑布局时选择要预览的布局。我刚刚添加了 id
  • 我已经修复了添加:tools:layout="@layout/fragment_news"> 但那是做什么用的呢?
  • 我不知道你可以检查文档
  • 您为什么要对问题进行编辑,只对格式进行细微的更改?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 2015-02-02
  • 1970-01-01
  • 2012-10-13
相关资源
最近更新 更多