正确设置 ListFragment
这是我第一次使用一般的片段,所以请耐心等待。我真的不明白我需要做什么才能使列表正确显示,因为到目前为止,当我在程序中选择选项卡时没有任何显示。目前我收到运行时错误
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;
}
}
This is my first time using fragments in general so bear with me. I dont really understand what I need to do to get the list to show correctly because as of right now nothing shows when I select a tab in my program. Currently I am getting a runtime error
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)
this is part of my 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>
this is my 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在
Fragment
内执行Fragment
。不支持嵌套片段。You cannot do
Fragment
inside aFragment
. Nested Fragments are not supported.