【发布时间】: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