Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

view pager and listview

Does anyone know how and why this one happen?

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)

What I did was

  1. open my sherlockfragmentactivity with a view pager

  2. click an item on listview inside the viewpager which will take me to the second activity

  3. press button back which will return us to the activity with viewpager in it.

First action on that three steps is fine, however when I repeat the second step and the third step several time fast, sometime it will cause that problem.

Any idea how to fix it?

This is my code :

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];
    }

}

//========= Layout for the activity which has view pager in it

<?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>

/==== Layout for my fragment in the view pager ===

<?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>
like image 348
Krishna Dewabrata Avatar asked Aug 29 '13 03:08

Krishna Dewabrata


1 Answers

in my case the problem was related to setting the wrong root container for the header of the listview while inflating:

WRONG CODE CAUSING ERROR:

transparentHeaderView = LayoutInflater.from(getActivity()).inflate(R.layout.transparent_header_view, activityContainer, false);

CORRECT CODE:

transparentHeaderView = LayoutInflater.from(getActivity()).inflate(R.layout.transparent_header_view, listView, false);

as @Krishna Dewabrata hinted, his problem was probably cause by doing something similar with the footer of the listview

like image 104
Giorgio Avatar answered Sep 28 '22 15:09

Giorgio