【问题标题】:java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.app.Fragmentjava.lang.ClassCastException: android.widget.LinearLayout 不能转换为 android.support.v4.app.Fragment
【发布时间】:2015-03-07 04:23:44
【问题描述】:

当我尝试在我的应用程序中打开我的活动CuteCollection.java 时,我收到此错误java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.app.Fragment。奇怪的是,当我单击 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)

【问题讨论】:

  • 我不清楚你的问题是什么。我认为您只想先解决运行时错误。然后处理您的设计。为此,我将发布一个答案,因为我认为这是 Fragments 的常见错误。
  • 我在“android.support.v4.app.Fragment”上更新了我最早的答案,并发布了另一个答案供您决定工作示例和设计。

标签: android-fragments tabs classcastexception fragmentpageradapter pagerslidingtabstrip


【解决方案1】:

除非您修复异常错误,否则您当前的代码似乎遇到了问题。 Google 提供了一个工作示例代码,这与您的代码设计非常相似,符合您的愿景。我已经尝试过了,它有效。链接SlidingTabsBasic。一个警告是您将不得不更改 Gradle 构建文件。如果你选择这条路线,那么我可以发布我的构建文件。

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

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

【讨论】:

  • 感谢@TOA!我能够让使用标签创建滑动视图以最低限度地工作,只显示标签,但是,它们不会显示我的片段视图。我在代码中注意到,它们不使用 xml,它们只显示每个标签页放入 TextView 的文本。在我的应用程序中,每个页面都只是一个GridView,但选项卡的每个片段都有自己需要使用的布局。另一个问题是我需要对标签进行样式化,所以不确定如何。有什么方法可以帮助我聊天吗? (当然,当你有机会时)。我也不能滑动标签,只能点击它们来改变它们。
  • 我很高兴你在进步,不再陷入模糊的 xml 文件错误。上面提到的项目(我用过)效果很好。我使用该项目在具有多列的选项卡上填充 ListViews,并且它是可滑动的。
  • 如果您还有其他问题,而且看起来确实如此,请将其发布到 Stackoverflow 上,并带有 listview、android-fragments 和 Android 的标签。与聊天不同,这就是我们建立社区和分享信息的方式。我会检查。同时,您可以选择此作为正确且有帮助的答案。
  • 我宁愿不共享代码,因为我的应用程序还没有准备好上市并且我保密。上面的项目很好。选项卡中缺少 UI 元素(现在我记得了)。为此,您可以搜索如何在片段中制作 UI。这将需要您制作 XML 布局。
  • 是的,这是真的。当你从初学者到中级时,这是最困难的部分。涉及的变量太多,很难获得帮助。 :( 但我的技术还不够熟练,经常靠自己解决。
【解决方案2】:

可能有 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 "com.android.support:support-v4..."

【讨论】:

  • 谢谢,我把每一个有android.app.Fragment的activity都改成了android.support.v4.app.Fragment,但是还是会出现这个错误。我发布所有详细信息的原因是因为我认为它可能与它们有关,并且在 Stack 上找不到任何东西来解决我的问题,因此需要详细信息。我只是不知道错误可能意味着什么。
  • 由于这是一个运行时错误(假设),您能否通过调试器单步执行代码并告诉我们您失败的代码行?您可以更新您的代码,以减少我们之间的误解。我会检查的。
  • 谢谢,非常感谢您的帮助。我一直试图让这个PagerSlidingTabStrip 工作好几天。我更新了我的代码。我改变了一些东西。我认为最好将CuteCollection.java 从我的导航抽屉中取出,这样它就可以变成Activity,而不是Fragment。为了能够使用来自FragmentActivity(位于ActionBarActivity 内部)的getSupportFragmentManager(),我不得不这样做,这是适配器将标签条连接到我的视图所必需的。由于选项卡都是片段。很想知道你的想法,谢谢。
  • 我没有看到任何会导致 ClassCastException 运行时错误的代码问题。你能说出哪个代码导致了异常吗?另一件事是,您正在使用包“com.astuetz.PagerSlidingTabStrip”,也许这个包/库存在兼容性问题。
  • 如果您仍在追求您的代码,我将在您的构建中添加一个可能的问题。我只是引用 Gradle。
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多