0

我正在尝试为 2 个选项卡创建 2 个选项卡和 2 个不同的活动。但在启动活动期间抛出错误。这是我的代码:

public class CashSales extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.cash_sales_tab);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                CustomerAddActivity.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                ItemAddActivity.class, null);
    }
}

这是我的活动,将出现在一个选项卡中。

   public class CustomerAddActivity extends FragmentActivity {
        private EditText mName; 
        private EditText mEmail1;
        private Integer mPkid;

        @Override
        protected void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            setContentView(R.layout.activity_customer_add);

            mName = (EditText) findViewById(R.id.customer_add_name);
            mEmail1 = (EditText) findViewById(R.id.customer_add_email1);
            Button confirmButton = (Button) findViewById(R.id.customer_add_button);
'''''''''''''''''''''''''''         
        }
    }

目录日志:

02-26 05:52:39.732: E/AndroidRuntime(25579): FATAL EXCEPTION: main
02-26 05:52:39.732: E/AndroidRuntime(25579): java.lang.ClassCastException: asia.wavelet.android.customer.CustomerAddActivity cannot be cast to android.support.v4.app.Fragment
02-26 05:52:39.732: E/AndroidRuntime(25579):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
02-26 05:52:39.732: E/AndroidRuntime(25579):    at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339)
02-26 05:52:39.732: E/AndroidRuntime(25579):    at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:276)
4

2 回答 2

1

您在addTab这里为该方法提供了一个活动而不是一个片段:

mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                CustomerAddActivity.class, null);
 mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                ItemAddActivity.class, null);

这就是系统试图将活动转换为片段并抛出ClassCastException.

于 2013-02-26T06:18:02.447 回答
0

你应该扩展 Fragment,而不是 Fragment Activity。

像这样。

public class CustomerAddActivity extends Fragment
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_customer_add, container, false);
    }
于 2013-02-26T06:18:09.193 回答