java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.support.v4.app.Fragment

时间:2015-03-07 04:23:44

标签: android-fragments tabs classcastexception fragmentpageradapter pagerslidingtabstrip

当我尝试在我的应用中打开我的活动java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.app.Fragment时,我收到此错误CuteCollection.java。奇怪的是,当我在logcat错误中单击第一行(FragmentPagerAdapter.java:122)时,它会显示v4.support库中文件的一行。我无法编辑该代码,因此我的代码中必须有一些内容可以更改。

要开始此活动,我点击了我的HomeFragment.java片段中的一个按钮,这是我导航抽屉中的一个片段,它也有android.support.v4.app.Fragment扩展名,就像我导航中的所有项目一样抽屉。

我认为这可能与我的FragmentPagerAdpater有关。虽然我确实将所有android.app.Fragment更改为android.support.v4.app.Fragment,但仍然存在相同的错误。

更新:当我点击support.v4库中名为FragmentPagerAdapter的第一行时,会显示该类并显示Fragment fragment = (Fragment)object;突出显示,这是此方法的一部分(尽管我无法编辑,因为它来自Android):

@Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment)object;
        if (fragment != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                mCurrentPrimaryItem.setMenuVisibility(false);
                mCurrentPrimaryItem.setUserVisibleHint(false);
            }
            if (fragment != null) {
                fragment.setMenuVisibility(true);
                fragment.setUserVisibleHint(true);
            }
            mCurrentPrimaryItem = fragment;
        }
    }

任何提示或建议?感谢。

CuteCollectionFragment.java

    package org.azurespot.cutecollection;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;

import org.azurespot.R;

/**
 * Created by mizu on 1/26/15.
 */
public class CuteCollection extends ActionBarActivity {

    private static final int PHOTO_TAB = 0;
    private static final int VIDEO_TAB = 1;
    private static final int AUDIO_TAB = 2;
    private static final int TEXT_TAB = 3;

    PhotoTab photoTab;
    TextTab textTab;
    VideoTab videoTab;
    AudioTab audioTab;


    public CuteCollection(){}

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

        // Instantiate tabs
        photoTab = new PhotoTab();
        textTab = new TextTab();
        videoTab = new VideoTab();
        audioTab = new AudioTab();

        // Initialize the ViewPager and set an adapter
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(new TabsAdapter(getSupportFragmentManager()));

        // Bind the tabs to the ViewPager
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip)
                                            findViewById(R.id.tabs);
        tabs.setViewPager(pager);

    }

    private class TabsAdapter extends FragmentPagerAdapter {

        public TabsAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * @return the number of pages (tabs) to display
         */
        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Photos";
                case 1:
                    return "Videos";
                case 2:
                    return "Sounds";
                case 3:
                    return "Poems";
            }

            return null;
        }

        /**
         * @return true if the value returned from
         *         {@link #instantiateItem(ViewGroup, int)} is the same object
         *         as the {@link View} added to the {@link ViewPager}.
         */
        @Override
        public boolean isViewFromObject(View view, Object o) {
            return o == view;
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {

            switch(position){
                case PHOTO_TAB:
                    Bundle photoBundle = new Bundle();
                    photoBundle.putInt("page_position", position + 1);
                    PhotoTab pt = new PhotoTab();
                    pt.setArguments(photoBundle);
                    return pt;
                case VIDEO_TAB :
                    Bundle videoBundle = new Bundle();
                    videoBundle.putInt("page_position", position + 1);
                    VideoTab vt = new VideoTab();
                    vt.setArguments(videoBundle);
                    return new VideoTab();
                case AUDIO_TAB:
                    Bundle audioBundle = new Bundle();
                    audioBundle.putInt("page_position", position + 1);
                    AudioTab at = new AudioTab();
                    at.setArguments(audioBundle);
                    return new AudioTab();
                case TEXT_TAB:
                    Bundle textBundle = new Bundle();
                    textBundle.putInt("page_position", position + 1);
                    TextTab tt = new TextTab();
                    tt.setArguments(textBundle);
                    return new TextTab();
            }

            return null;

        }

        /**
         * Instantiate the {@link View} which should be displayed at
         * {@code position}. Here we inflate a layout from the apps resources
         * and then change the text view to signify the position.
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // Inflate a new layout from our resources

            View view = getLayoutInflater().inflate(R.layout.pager_item,
                    container, false);
            // Add the newly created View to the ViewPager
            container.addView(view);

            // Retrieve a TextView from the inflated View, and update it's text
            TextView title = (TextView) view.findViewById(R.id.item_title);
            title.setText(String.valueOf(position));

            // Return the View
            return view;
        }

        /**
         * Destroy the item from the {@link ViewPager}. In our case this is
         * simply removing the {@link View}.
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}

fragment_cute_collection.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2198bb" >

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        custom:pstsTextColorSelected="#ffffff"
        custom:pstsUnderlineColor="#ffffff"
        custom:pstsIndicatorColor="#ffffff"
        android:textColor="#2198bb"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:background="@android:color/white" />

</RelativeLayout>

logcat的

03-07 18:35:42.669    6340-6340/org.azurespot E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.azurespot, PID: 6340
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.app.Fragment
        at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:122)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:17619)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
        at android.view.View.measure(View.java:17619)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17619)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:17619)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17619)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17619)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2588)
        at android.view.View.measure(View.java:17619)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2317)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1412)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1613)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1270)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6691)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
        at android.view.Choreographer.doCallbacks(Choreographer.java:613)
        at android.view.Choreographer.doFrame(Choreographer.java:583)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5731)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

除非您修复了Exception错误,否则您当前的代码似乎遇到了麻烦。 Google提供了一个工作示例代码,这与您的代码设计非常相似,可以满足您的愿景。我试过了,它有效。链接SlidingTabsBasic。一个警告是您必须更改Gradle构建文件。如果您选择此路线,那么我可以发布我的构建文件。

同一网页上的另一个类似示例是 SlidingTabsColors ,这听起来不错,因为我最终为我制作的任何GUI自定义颜色。如果您安装了SDK,则可能在本地驱动器上提供。 如果你决定这条路线,只需接受它作为最佳答案,并发布你可能提出的任何问题。至少,我知道示例代码有效。

只是为了让你知道,Creating Swipe Views with Tabs有一个有趣的阅读,它符合你的愿景,代码似乎比我上面推荐的更简单。但是......它使用了ActionBar.TabListener,它已被弃用于Lollipop,API版本21.您的选择......

答案 1 :(得分:0)

可能有2种不同的解决方案/部件。 第1部分) 在发布的Java文件中,执行以下操作:

import android.support.v4.app.Fragment ;

删除或注释掉:

import android.app.Fragment;

原因是您正在使用 android.support.v4.app.Fragment 。这与您当前的导入 android.app.Fragment 不兼容。此外,您的所有导入都引用 android.support.v4 ,因此您知道无论如何都应该这样做以保持一致:-) 不幸的是,编译器没有检测到这种不兼容性。

第2部分) 由于您正在使用ActionBarActivity,因此构建文件或设置可能存在问题。 Stackoverflow @ ActionBarActivity cannot resolve a symbolError inflating class from library中的其他人已确认此问题。我知道你的错误听起来不一样,但我认为问题可能是一样的。基本上你可以查看你的构建gradle文件(假设你正在使用它):

dependencies {
    compile "com.android.support:support-v4:21.0.2"
}

这个想法是使用compile&#34; com.android。支持:support-v4 ...&#34;