滑动+标签android类强制转换异常

时间:2014-02-24 17:49:31

标签: java android android-fragments android-tabhost android-fragmentactivity

我试图在android中实现滑动+标签,但我现在有这个问题。当我启动我的应用程序时,我在logCat中得到它:

02-24 18:42:16.062: E/AndroidRuntime(12322): FATAL EXCEPTION: main
02-24 18:42:16.062: E/AndroidRuntime(12322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.TabsViewPagerFragmentActivity}: java.lang.ClassCastException: com.test.ScanQR cannot be cast to android.support.v4.app.Fragment
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.os.Looper.loop(Looper.java:137)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.ActivityThread.main(ActivityThread.java:4441)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at java.lang.reflect.Method.invoke(Method.java:511)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at dalvik.system.NativeStart.main(Native Method)
02-24 18:42:16.062: E/AndroidRuntime(12322): Caused by: java.lang.ClassCastException: com.test.ScanQR cannot be cast to android.support.v4.app.Fragment
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at com.test.TabsViewPagerFragmentActivity.intialiseViewPager(TabsViewPagerFragmentActivity.java:123)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at com.test.TabsViewPagerFragmentActivity.onCreate(TabsViewPagerFragmentActivity.java:103)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.Activity.performCreate(Activity.java:4465)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-24 18:42:16.062: E/AndroidRuntime(12322):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
02-24 18:42:16.062: E/AndroidRuntime(12322):    ... 11 more

这是我的代码:

public class TabsViewPagerFragmentActivity extends FragmentActivity implements
        TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>();
    private PagerAdapter mPagerAdapter;

    /**
     * 
     * @author mwho Maintains extrinsic info of a tab's construct
     */
    private class TabInfo {
        private String tag;
        private Class<?> clss;
        private Bundle args;
        private Fragment fragment;

        TabInfo(String tag, Class<?> clazz, Bundle args) {
            this.tag = tag;
            this.clss = clazz;
            this.args = args;
        }

    }

    /**
     * A simple factory that returns dummy views to the Tabhost
     * 
     * @author mwho
     */
    class TabFactory implements TabContentFactory {

        private final Context mContext;

        /**
         * @param context
         */
        public TabFactory(Context context) {
            mContext = context;
        }

        /**
         * (non-Javadoc)
         * 
         * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
         */
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }

    }

    /**
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Inflate the layout
        setContentView(R.layout.tabs_viewpager_layout);
        // Initialise the TabHost
        this.initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); // set
                                                                                // the
                                                                                // tab
                                                                                // as
                                                                                // per
                                                                                // the
                                                                                // saved
                                                                                // state
        }
        // Intialise ViewPager
        this.intialiseViewPager();
    }

    /**
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
     */
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
                                                                // selected
        super.onSaveInstanceState(outState);
    }

    /**
     * Initialise ViewPager
     */
    private void intialiseViewPager() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, ScanQR.class.getName()));
        fragments.add(Fragment.instantiate(this, InsertCode.class.getName()));
        fragments.add(Fragment.instantiate(this, Guide.class.getName()));
        this.mPagerAdapter = new PagerAdapter(
                super.getSupportFragmentManager(), fragments);
        //
        this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
        this.mViewPager.setAdapter(this.mPagerAdapter);
        this.mViewPager.setOnPageChangeListener(this);
    }

    /**
     * Initialise the Tab Host
     */
    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost
                .newTabSpec("Tab1").setIndicator("Tab 1"),
                (tabInfo = new TabInfo("Tab1", ScanQR.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost
                .newTabSpec("Tab2").setIndicator("Tab 2"),
                (tabInfo = new TabInfo("Tab2", InsertCode.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost
                .newTabSpec("Tab3").setIndicator("Tab 3"),
                (tabInfo = new TabInfo("Tab3", Guide.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        // Default to first tab
        // this.onTabChanged("Tab1");
        //
        mTabHost.setOnTabChangedListener(this);
    }

    /**
     * Add Tab content to the Tabhost
     * 
     * @param activity
     * @param tabHost
     * @param tabSpec
     * @param clss
     * @param args
     */
    private static void AddTab(TabsViewPagerFragmentActivity activity,
            TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        // Attach a Tab view factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    /**
     * (non-Javadoc)
     * 
     * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
     */
    public void onTabChanged(String tag) {
        // TabInfo newTab = this.mapTabInfo.get(tag);
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled
     * (int, float, int)
     */
    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected
     * (int)
     */
    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        this.mTabHost.setCurrentTab(position);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#
     * onPageScrollStateChanged(int)
     */
    @Override
    public void onPageScrollStateChanged(int state) {
        // TODO Auto-generated method stub

    }
}

和ScanQR类

public class ScanQR extends FragmentActivity implements OnQRCodeReadListener {

    private QRCodeReaderView mydecoderview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scan_qr);

        mydecoderview = (QRCodeReaderView) findViewById(R.id.qrdecoderview);
        mydecoderview.setOnQRCodeReadListener(this);
    }

    // Called when a QR is decoded
    // "text" : the text encoded in QR
    // "points" : points where QR control points are placed
    @Override
    public void onQRCodeRead(String text, PointF[] points) {
        // myTextView.setText(text);
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    // Called when your device have no camera
    @Override
    public void cameraNotFound() {

    }

    // Called when there's no QR codes in the camera preview image
    @Override
    public void QRCodeNotFoundOnCamImage() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mydecoderview.getCameraManager().startPreview();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mydecoderview.getCameraManager().stopPreview();
    }

}

0 个答案:

没有答案