1

我正在尝试实现 FragmentTabPager Activity,但它根本没有运行。一旦我调用该活动,应用程序就会立即停止。我已经尝试过多种代码/教程来源,包括developers.android,但同样的问题发生在所有这些来源中。据我了解,没有人遇到过同样的问题(至少他们没有写过)。这让我相信我犯了一些愚蠢的错误。我在这上面花了 2 天多的时间。这是我的 fragmentTabPager 活动的代码:

package com.my.pack;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;

import java.util.ArrayList;

public class FragmentTabsPager extends FragmentActivity {
    TabHost mTabHost;
    ViewPager mViewPager;
    TabsAdapter mTabsAdapter;

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

        setContentView(R.layout.tabs_viewpager_layout);
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mViewPager = (ViewPager) findViewById(R.id.pager);

        // Create our tab adapter
        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

        // add our tabs to the adapter
        mTabsAdapter.addTab(mTabHost.newTabSpec("forex").setIndicator("Forex"),
                Forex.class, null);
        mTabsAdapter.addTab(mTabHost.newTabSpec("stocks")
                .setIndicator("Stocks"), Stocks.class, null);
        mTabsAdapter.addTab(
                mTabHost.newTabSpec("commodities").setIndicator("Commodities"),
                Commodities.class, null);
        /*
         * mTabsAdapter.addTab(mTabHost.newTabSpec("throttle").setIndicator(
         * "Throttle"), LoaderThrottleSupport.ThrottledLoaderListFragment.class,
         * null);
         */

        if (savedInstanceState != null) {
            // restore the last selected tab if we can
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

    public static class TabsAdapter extends FragmentPagerAdapter implements
            TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        private final ArrayList mTabs = new ArrayList();

        static final class TabInfo {
            private final String tag;
            private final Class clss;
            private final Bundle args;

            TabInfo(String _tag, Class _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabsAdapter(FragmentActivity activity, TabHost tabHost,
                ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            TabInfo info = (TabInfo) mTabs.get(position);
            // Create a new fragment if necessary.
            return Fragment.instantiate(mContext, info.clss.getName(),
                    info.args);
        }

        public void onTabChanged(String tabId) {
            // called when the user clicks on a tab.
            int position = mTabHost.getCurrentTab();
            mViewPager.setCurrentItem(position);
        }

        public void onPageScrolled(int position, float positionOffset,
                int positionOffsetPixels) {
        }

        public void onPageSelected(int position) {

            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }

        public void onPageScrollStateChanged(int state) {
        }
    }
}

我的清单看起来像:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.pack"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".FragmentTabsPager"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.FRAGMENTTABSPAGER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Stocks"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.STOCKS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Commodities"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.COMMODITIES" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Forex"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.FOREX" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

tabs_viewpager_layout 文件的xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

我希望从中调用我的 fragmentTab 活动的主要内容:

package com.my.pack;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class Main extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent myintent = new Intent(Main.this, FragmentTabsPager.class);
                startActivity(myintent);
            }
        });
    }

}

我知道我的 3 个外汇、股票等寻呼机活动运行顺利。我通过将 main 中的意图更改为 Forex.class 进行了检查,并且所有 3 个都可以正常工作。我还认为该活动必须是 LAUNCHER 活动,但尝试了该活动却失败了。提前致谢。任何帮助将不胜感激。

我的日志猫是..

04-08 19:05:24.839: D/AndroidRuntime(1332): Shutting down VM
04-08 19:05:24.839: W/dalvikvm(1332): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-08 19:05:24.869: E/AndroidRuntime(1332): FATAL EXCEPTION: main
04-08 19:05:24.869: E/AndroidRuntime(1332): java.lang.ClassCastException: com.my.pack.Forex
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.support.v4.app.Fragment.instantiate(Fragment.java:384)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at com.my.pack.FragmentTabsPager$TabsAdapter.getItem(FragmentTabsPager.java:124)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:95)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.os.Looper.loop(Looper.java:123)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at java.lang.reflect.Method.invoke(Method.java:521)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-08 19:05:24.869: E/AndroidRuntime(1332):     at dalvik.system.NativeStart.main(Native Method)
04-08 19:05:27.898: I/Process(1332): Sending signal. PID: 1332 SIG: 9
4

1 回答 1

1

android:id tags在这三个活动中是否有重复?我读过这种情况可能会导致问题。

啊,这是我读到的链接: ClassCastException

于 2012-04-08T14:45:50.237 回答