Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException: android.widget.LinearLayout$LayoutParams

Tags:

android

layout

So i get this error when executing an adapter in FinderActivity.class.

public class FinderActivity extends ListActivity
{
    private List<String> itemsearchoptions = Arrays.asList(
            new String[]
                    {
                        "Search Service Center","Search Showroom",
                        "Area Surveillance", "Nearest OAC Offices"
                    });
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finder);


        getListView().setAdapter(new FinderListAdapter(this, this.itemsearchoptions));//cause of error


    }

Here is My FinderAdapterclass:

public class FinderListAdapter extends BaseAdapter
{
    Context context;
    LayoutInflater inflater;
    private int[]icons = 
    {
        R.drawable.icon_service_center,
        R.drawable.icon_car_showroom, 
        R.drawable.icon_radar, 
        R.drawable.icon_office
    };
    private List<String>options = null;

    public FinderListAdapter(Context paramContext, List<String>paramList)
    {
        this.context = paramContext;
        this.options = paramList;
        this.inflater = LayoutInflater.from(paramContext);
    }

    @Override
    public int getCount() 
    {
        return this.options.size();
    }

    @Override
    public Object getItem(int paramInt) 
    {
        return this.options.get(paramInt);
    }

    @Override
    public long getItemId(int paramInt) 
    {
        return paramInt;
    }


    @Override
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) 
    {
        String str = (String)this.options.get(paramInt);

        if(paramView == null)
        {

        }
        else
        {

            return paramView;
        }
        return paramViewGroup;
    }
    public boolean isEmpty()
    {
        return (this.options == null)||(this.options.size()==0);
    }

    private static class ViewHolder
    {
        ImageView finderIcon;
        TextView finderText;
    }
}

I used LinearLayout on the item list

I have read several similar problems about this error, but yet to fully understand the concept. So any help will be appreciated. thanks

edited : sorry forgot the stacktrace

07-08 15:31:42.380: E/AndroidRuntime(4550): FATAL EXCEPTION: main
07-08 15:31:42.380: E/AndroidRuntime(4550): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.widget.ListView.measureScrapChild(ListView.java:1166)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1248)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.widget.ListView.onMeasure(ListView.java:1158)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.view.View.measure(View.java:15525)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.view.View.measure(View.java:15525)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.view.View.measure(View.java:15525)
07-08 15:31:42.380: E/AndroidRuntime(4550):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
like image 726
Karate_Dog Avatar asked Jul 08 '13 09:07

Karate_Dog


2 Answers

Use android.widget.AbsListView.LayoutParams instead of the android.widget.LinearLayout.LayoutParams in your imports.

A ClassCastException stacktrace is pretty descriptive...

like image 76
WarrenFaith Avatar answered Oct 16 '22 07:10

WarrenFaith


For the rest of you which have the same problem. Please at first, fix your getView method.

Try to make it like this.

public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View root;

    if(view == null){
       root = layoutInflater.inflate(R.layout.yourLayout, null);
    } else {
       root = view;
    }

    return root;
    }

This may solve the android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams error.

Trust me, I just facing this exact problem, but I manage to solve it this way.

like image 36
Azizi Musa Avatar answered Oct 16 '22 05:10

Azizi Musa