java.lang.ClassCastException: android.widget.LinearLayout 无法转换为 android.support.v4.app.Fragment
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.app.Fragment
当我尝试在我的应用程序中打开 activity CuteCollection.java
时出现此错误 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.app.Fragment
。奇怪的是,当我单击 logcat 错误中的第一行 (FragmentPagerAdapter.java:122)
时,它会显示 v4.support
库中文件中的一行。我无法编辑该代码,所以我的代码中一定有我可以更改的内容。
为了到达这个 activity,我单击 HomeFragment.java
片段中的一个按钮,它是我的导航抽屉中的一个片段,它也有 android.support.v4.app.Fragment
扩展名,就像所有项目都在我的导航抽屉里。
我认为这可能与我的 FragmentPagerAdpater
有关。虽然我确实把我所有的 android.app.Fragment
改成了 android.support.v4.app.Fragment
,但是还是一样的错误。
更新:当我单击 support.v4
库中的第一行,称为 FragmentPagerAdapter
,它会弹出 class 并突出显示 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 个不同 solutions/parts。
第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,构建文件或设置可能存在问题。 Whosebug @ ActionBarActivity cannot resolve a symbol and Error inflating class from library 中的其他人确认了该问题。我知道你的错误听起来不同,但我认为问题可能是一样的。基本上你可以检查你的构建 gradle 文件(假设你正在使用它):
dependencies {
compile "com.android.support:support-v4:21.0.2"
}
思路是使用编译"com.android.support:support-v4..."
您当前的代码似乎 运行 有问题,除非您修复异常错误。 Google 中有一个有效的示例代码,这与您的代码设计非常相似,符合您的设想。我已经尝试过了,而且有效。 Link 在 SlidingTabsBasic。一个警告是您必须更改 Gradle 构建文件。如果你选择这条路线,那么我可以 post 我的构建文件。
同一网页上的另一个类似示例是 SlidingTabsColors,这听起来不错,因为我最终会为我制作的任何 GUI 自定义颜色。如果您安装了 SDK,则这些 SDK 可能在您的本地驱动器上可用。
如果您决定采用这条路线,请将其作为最佳答案,并且 post 无论您遇到什么问题。至少,我知道示例代码有效。
顺便告诉你,Creating Swipe Views with Tabs有一个有趣的阅读,也符合你的设想,而且代码似乎比我上面推荐的要简单。但是...它使用 ActionBar.TabListener,Lollipop 已弃用 API 版本 21。您的选择...
当我尝试在我的应用程序中打开 activity CuteCollection.java
时出现此错误 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.app.Fragment
。奇怪的是,当我单击 logcat 错误中的第一行 (FragmentPagerAdapter.java:122)
时,它会显示 v4.support
库中文件中的一行。我无法编辑该代码,所以我的代码中一定有我可以更改的内容。
为了到达这个 activity,我单击 HomeFragment.java
片段中的一个按钮,它是我的导航抽屉中的一个片段,它也有 android.support.v4.app.Fragment
扩展名,就像所有项目都在我的导航抽屉里。
我认为这可能与我的 FragmentPagerAdpater
有关。虽然我确实把我所有的 android.app.Fragment
改成了 android.support.v4.app.Fragment
,但是还是一样的错误。
更新:当我单击 support.v4
库中的第一行,称为 FragmentPagerAdapter
,它会弹出 class 并突出显示 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 个不同 solutions/parts。 第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,构建文件或设置可能存在问题。 Whosebug @ ActionBarActivity cannot resolve a symbol and Error inflating class from library 中的其他人确认了该问题。我知道你的错误听起来不同,但我认为问题可能是一样的。基本上你可以检查你的构建 gradle 文件(假设你正在使用它):
dependencies {
compile "com.android.support:support-v4:21.0.2"
}
思路是使用编译"com.android.support:support-v4..."
您当前的代码似乎 运行 有问题,除非您修复异常错误。 Google 中有一个有效的示例代码,这与您的代码设计非常相似,符合您的设想。我已经尝试过了,而且有效。 Link 在 SlidingTabsBasic。一个警告是您必须更改 Gradle 构建文件。如果你选择这条路线,那么我可以 post 我的构建文件。
同一网页上的另一个类似示例是 SlidingTabsColors,这听起来不错,因为我最终会为我制作的任何 GUI 自定义颜色。如果您安装了 SDK,则这些 SDK 可能在您的本地驱动器上可用。 如果您决定采用这条路线,请将其作为最佳答案,并且 post 无论您遇到什么问题。至少,我知道示例代码有效。
顺便告诉你,Creating Swipe Views with Tabs有一个有趣的阅读,也符合你的设想,而且代码似乎比我上面推荐的要简单。但是...它使用 ActionBar.TabListener,Lollipop 已弃用 API 版本 21。您的选择...