正确设置ListFragment

时间:2011-11-17 21:23:41

标签: android list fragment

这是我第一次使用片段,所以请耐心等待。我真的不明白我需要做什么才能使列表正确显示,因为当我在程序中选择一个选项卡时,现在没有任何显示。目前我收到运行时错误

11-17 16:18:59.410: E/AndroidRuntime(18219): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.bowling/com.tyczj.bowling.Tabs}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.tyczj.bowling.BowlersList$BowlersListFragment: make sure class name exists, is public, and has an empty constructor that is public
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.ActivityThread.access$1500(ActivityThread.java:122)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.os.Looper.loop(Looper.java:132)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.ActivityThread.main(ActivityThread.java:4028)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at java.lang.reflect.Method.invokeNative(Native Method)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at java.lang.reflect.Method.invoke(Method.java:491)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at dalvik.system.NativeStart.main(Native Method)
11-17 16:18:59.410: E/AndroidRuntime(18219): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.tyczj.bowling.BowlersList$BowlersListFragment: make sure class name exists, is public, and has an empty constructor that is public
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.Fragment.instantiate(Fragment.java:504)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at com.tyczj.bowling.Tabs$TabListener.onTabSelected(Tabs.java:110)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:483)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:303)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at com.android.internal.app.ActionBarImpl.setNavigationMode(ActionBarImpl.java:883)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at com.tyczj.bowling.Tabs.onCreate(Tabs.java:25)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
11-17 16:18:59.410: E/AndroidRuntime(18219):    ... 11 more
11-17 16:18:59.410: E/AndroidRuntime(18219): Caused by: java.lang.InstantiationException: com.tyczj.bowling.BowlersList$BowlersListFragment
11-17 16:18:59.410: E/AndroidRuntime(18219):    at java.lang.Class.newInstanceImpl(Native Method)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at java.lang.Class.newInstance(Class.java:1301)
11-17 16:18:59.410: E/AndroidRuntime(18219):    at android.app.Fragment.instantiate(Fragment.java:493)

这是我的xml的一部分

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="horizontal">
<LinearLayout android:layout_width="400dp"
    android:layout_height="match_parent">
    <fragment android:name="com.tyczj.bowling.BowlersList"
    android:id="@+id/bListFrag"
    android:layout_height="match_parent"
    android:layout_width="400dp"/>
</LinearLayout>
</LinearLayout>

这是我的ListFragment

public class BowlersList extends ListFragment {

@Override
public void onCreate(Bundle state){
    super.onCreate(state);

    if(getFragmentManager().findFragmentById(R.id.bListFrag)==null){
        BowlersListFragment list = new BowlersListFragment();
        getFragmentManager().beginTransaction().add(R.id.bListFrag, list).commit();
    }
}

public class BowlersListFragment extends Fragment {
      static public final String STATE_CHECKED="com.tyczj.bowling.STATE_CHECKED";

      @Override
      public void onActivityCreated(Bundle state){
          super.onActivityCreated(state);
          BowlersDB db = new BowlersDB(BowlersList.this);
          db.open();
          Cursor c = db.getBowlers();
          startManagingCursor(c);
          String[] names = new String[c.getCount()];
          if(c != null && c.moveToFirst()){
              int i = 0;
              do{
                  names[i] = c.getString(c.getColumnIndex(BowlersDB.NAME));
                i++;
              }while(c.moveToNext());
          }
      }

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.bowlers, container, false);
            return v;
        }
    }

1 个答案:

答案 0 :(得分:0)

您无法在Fragment内执行Fragment。不支持嵌套片段。