0

我在适用于 2.3.3 及更高版本的应用程序中使用 ActionBarSherlock。在我在片段选项卡主机中的应用程序中,我试图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{ }

日志猫

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)
4

1 回答 1

1

我试图用错误的方法解决问题。我用这个问题作为解决问题的指南。

于 2013-08-24T19:17:53.890 回答