有谁知道这一次发生的原因和原因?
java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.clearRecycledState(ListView.java:519)
at android.widget.ListView.resetList(ListView.java:506)
at android.widget.ListView.layoutChildren(ListView.java:1540)
at android.widget.AbsListView.onLayout(AbsListView.java:2296)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1141)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14055)
at android.view.ViewGroup.layout(ViewGroup.java:4604)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1992)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
我做的是
使用查看寻呼机打开我的sherlockfragmentactivity
点击viewpager中listview上的项目,这将带我进入第二项活动
按回按钮,将返回我们带有viewpager的活动。
关于这三个步骤的第一个动作很好,但是当我重复第二步和第三步快几次时,有时会导致这个问题。
知道怎么解决吗?
这是我的代码:
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import ex.godofsmith.helper.LocationService;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class RestoFragmentActivity extends SherlockFragmentActivity {
private Intent locationService;
private ViewPager _mViewPager;
private RestoVPAdapter _adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resto);
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
//set location service
locationService = new Intent(this, LocationService.class);
//end location service
setUpView();
}
@Override
public void onPause(){
super.onPause();
stopService(locationService);
}
@Override
public void onResume(){
super.onResume();
startService(locationService);
}
@Override
public void onDestroy(){
super.onDestroy();
//stopService(locationService);
}
private void setUpView(){
_adapter = new RestoVPAdapter(getApplicationContext(),getSupportFragmentManager());
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
_mViewPager.setOffscreenPageLimit(3);
PageIndicator mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(_mViewPager);
}
}
// ==================
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class RestoVPAdapter extends FragmentStatePagerAdapter {
//protected static final String[] CONTENT = new String[] { "Recommended", "Best", "Nearby", "Promo", "New"};
protected static final String[] CONTENT = new String[] { "Nearby", "Promo", "New"};
private Context _context;
private int mCount = CONTENT.length;
public RestoVPAdapter(Context context, FragmentManager fm) {
super(fm);
_context=context;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
// case 0:
// //f=RestoRecommendedActivity.newInstance(_context);
// break;
// case 1:
// //f=RestoBestActivity.newInstance(_context);
// break;
case 0:
f=RestoNearByActivity.newInstance(_context);
break;
case 1:
f=RestoPromoFragment.newInstance(_context);
break;
case 2:
f=RestoNewActivity.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return RestoVPAdapter.CONTENT[position % CONTENT.length];
}
}
// =========其中包含视图寻呼机的活动的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background">
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#FF000000"
app:footerColor="@color/greenslideup"
app:footerLineHeight="1dp"
app:footerIndicatorHeight="3dp"
app:footerIndicatorStyle="underline"
app:selectedColor="#FF000000"
app:selectedBold="true"/>
<ex.godofsmith.helper.CustomViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewPager" />
</LinearLayout>
/ ====视图分页器中我片段的布局===
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/lvNearBy"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
答案 0 :(得分:16)
在我的情况下,问题与在扩充时为列表视图的标题设置错误的根容器有关:
错误代码导致错误:
transparentHeaderView = LayoutInflater.from(getActivity()).inflate(R.layout.transparent_header_view, activityContainer, false);
正确代码:
transparentHeaderView = LayoutInflater.from(getActivity()).inflate(R.layout.transparent_header_view, listView, false);
@krishna Dewabrata暗示,他的问题可能是因为与listview的页脚做了类似的事情
答案 1 :(得分:0)
java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
这一行似乎说你可能在一个小部件上放置了一个布局,然后就无法进行转换。 但我不知道为什么它在第一次按下时有效。
尝试编辑您的布局文件,然后重试。