5

有谁知道这是如何以及为什么会发生的?

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)

我所做的是

  1. 使用视图寻呼机打开我的 sherlockfragmentactivity

  2. 单击 viewpager 内的 listview 上的一个项目,这将带我进入第二个活动

  3. 按下按钮返回,这将使我们返回到其中包含 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>
4

2 回答 2

17

在我的情况下,问题与在膨胀时为列表视图的标题设置错误的根容器有关:

错误代码导致错误:

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 暗示的那样,他的问题可能是由于对列表视图的页脚进行了类似的操作

于 2014-07-16T08:47:57.130 回答
0
java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

这行似乎是说您可能在一个小部件上放置了一个布局,然后它不能被投射。但我不知道为什么它在第一次按下时会起作用。

尝试编辑您的布局文件,然后重试。

于 2013-08-29T08:55:29.800 回答