扩展类片段时出错(As ListFragment)

时间:2012-07-07 11:21:02

标签: android

尝试在选项卡中创建ListFragment时出现以下错误。 我只是希望在那个片段中有一个列表,没有什么特别的......我很抱歉这是一个缺乏知识而不是一个bug,但我现在挂了2天这个问题,不知道我该做什么别的,然后问你们。 THX。

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" />

片段文件itselve包含:

<?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;
 }

2 个答案:

答案 0 :(得分:5)

在堆栈跟踪中

Caused by: java.lang.ClassCastException: com.fr34kyd3v.bucp.BanlistFragment cannot be cast to android.app.Fragment

我猜测,也许你的BanlistFragment类从支持库(android.support.v4.app.Fragment)扩展Fragment,并且活动尝试本地处理它&#34;没有支持库? 为了获得更多猜测,请提供BanlistFragment的源代码。

答案 1 :(得分: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或函数后调用...