3

尝试在选项卡中创建 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;
 }
4

2 回答 2

6

在堆栈跟踪中,您有:Caused by: java.lang.ClassCastException: com.fr34kyd3v.bucp.BanlistFragment cannot be cast to android.app.Fragment

我猜,也许您的 BanlistFragment 类从支持库(android.support.v4.app.Fragment)扩展了 Fragment,并且活动试图“本地处理”而不是支持库?为了获得比猜测更多的东西,请提供 BanlistFragment 的源代码。

于 2012-07-07T13:07:21.633 回答
1

点-1

它认为应该像 as 和 BanlistFragment 应该扩展 Fragment

  @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 tmp_view; //<------------------------------
     }

点-2

作为宠物生命周期http://developer.android.com/images/activity_fragment_lifecycle.png onCreate 在 onCreateView 之前被调用所以

setListAdapter(myListAdapter);

应该在 onCreateView 或在此之后调用函数...

于 2012-07-07T12:21:06.247 回答