我遇到了与片段相关的问题。我有一个闪屏,将会加载"持续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。
答案 0 :(得分: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"