我在我的应用程序中使用ActionBarSherlock,适用于2.3.3及更高版本。在我的片段标签主机中的应用程序中,我尝试在其中一个活动中实现ListView
,但我得到ClassCastException
。请找到附带的必要代码。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create FragmentTabHost
mTabHost = new FragmentTabHost(getSherlockActivity());
// Locate fragment1.xml to create FragmentTabHostx
mTabHost.setup(getSherlockActivity(), getChildFragmentManager(), R.layout.fragment1);
// Create Tab 1
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("ALL",
getResources().getDrawable(R.drawable.all)),
CreateEvent.class, null);
//mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(R.drawable.fb).setIndicator("Tab 1"), CreateEvent.class, null);
// Create Tab 2
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("NEARBY",getResources().getDrawable(R.drawable.near_by)), CreateEvent.class, null);
mTabHost.getTabWidget().setBackgroundColor(Color.GRAY);
return mTabHost;
}
CreateEvent.class
中有一个listView。
public class CreateEvent extends SherlockListActivity implements OnItemClickListener{
}
logcat的
08-22 18:37:48.237: E/AndroidRuntime(917): FATAL EXCEPTION: main
08-22 18:37:48.237: E/AndroidRuntime(917): java.lang.ClassCastException: com.android.myapp.events.CreateEvent
08-22 18:37:48.237: E/AndroidRuntime(917): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:276)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.View.dispatchAttachedToWindow(View.java:6156)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewRoot.performTraversals(ViewRoot.java:765)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.os.Looper.loop(Looper.java:123)
08-22 18:37:48.237: E/AndroidRuntime(917): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-22 18:37:48.237: E/AndroidRuntime(917): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 18:37:48.237: E/AndroidRuntime(917): at java.lang.reflect.Method.invoke(Method.java:507)
08-22 18:37:48.237: E/AndroidRuntime(917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-22 18:37:48.237: E/AndroidRuntime(917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-22 18:37:48.237: E/AndroidRuntime(917): at dalvik.system.NativeStart.main(Native Method)