尝试在选项卡中创建 ListFragment 时出现以下错误。我只是想在那个片段中有一个列表,没什么特别的.. 很抱歉,这更像是缺乏知识而不是错误,但我在这个问题上挂了 2 天,不知道我该怎么办否则再问你们。谢谢。
07-07 13:08:18.693: E/AndroidRuntime(19662): FATAL EXCEPTION: main
07-07 13:08:18.693: E/AndroidRuntime(19662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com..bucp/com..bucp.MainActivity}: android.view.InflateException: Binary XML file line #63: Error inflating class fragment
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.os.Looper.loop(Looper.java:137)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.ActivityThread.main(ActivityThread.java:4441)
07-07 13:08:18.693: E/AndroidRuntime(19662): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 13:08:18.693: E/AndroidRuntime(19662): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 13:08:18.693: E/AndroidRuntime(19662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-07 13:08:18.693: E/AndroidRuntime(19662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-07 13:08:18.693: E/AndroidRuntime(19662): at dalvik.system.NativeStart.main(Native Method)
07-07 13:08:18.693: E/AndroidRuntime(19662): Caused by: android.view.InflateException: Binary XML file line #63: Error inflating class fragment
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-07 13:08:18.693: E/AndroidRuntime(19662): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.Activity.setContentView(Activity.java:1835)
07-07 13:08:18.693: E/AndroidRuntime(19662): at com..bucp.MainActivity.onCreate(MainActivity.java:21)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.Activity.performCreate(Activity.java:4465)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
07-07 13:08:18.693: E/AndroidRuntime(19662): ... 11 more
07-07 13:08:18.693: E/AndroidRuntime(19662): Caused by: java.lang.ClassCastException: com..bucp.BanlistFragment cannot be cast to android.app.Fragment
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.Fragment.instantiate(Fragment.java:574)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.Fragment.instantiate(Fragment.java:549)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.app.Activity.onCreateView(Activity.java:4235)
07-07 13:08:18.693: E/AndroidRuntime(19662): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
07-07 13:08:18.693: E/AndroidRuntime(19662): ... 25 more
给定的第 63 行是片段的标签:
<fragment
android:name="com..bucp.BanlistFragment"
android:id="@+id/banl_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/banlist_fragment" />
片段文件本身包含:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
而片段类只包含
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListAdapter myListAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
month);
setListAdapter(myListAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View tmp_view = inflater.inflate(R.layout.banlist_fragment, container, false);
ListView list = (ListView) tmp_view.findViewById(android.R.id.list);
return list;
}