android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

Hetiwos picture Hetiwos · Nov 19, 2014 · Viewed 20.4k times · Source

Following numerous tutorials and example projects I've arrived at the following code which should fill a ListView with rows that contain multiple values out of a Line object.

Being a newbie programmer I cannot for the life of me figure out where the hell my code fails. Any help would be much appreciated!

If I left out important information please tell me and I will update this question.

The code that starts it all inside the Fragment:

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        Intent i = getActivity().getIntent();

        if (i.getSerializableExtra("ChosenWorkorder") != null){

            Workorder selectedWorkorder = (Workorder) i.getSerializableExtra("ChosenWorkorder");                

            ArticlesListAdapter articlesListAdapter = new ArticlesListAdapter(getActivity(), R.layout.articles_list_row, selectedWorkorder.GetAllLines());

            ListView articlesList = (ListView) getActivity().findViewById(R.id.ArticlesList);
            articlesList.setAdapter(articlesListAdapter);

        }
    }

The Custom ArrayAdapter:

    public class ArticlesListAdapter extends ArrayAdapter<Line> {

    private List<Line> lineList;
    private Context context;

    public ArticlesListAdapter(Context context, int textViewResourceId, List<Line> objects) {
    super(context, textViewResourceId, objects);
        this.lineList = objects;
        this.context = context;
    }

    public int getCount() {
        return lineList.size();
    }

    public Line getItem(int position) {
        return lineList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder") /* Que? */
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.articles_list_row, parent, false);

        TextView articleName = (TextView)     rowView.findViewById(R.id.textArticleName);
        EditText articleAmount = (EditText)     rowView.findViewById(R.id.textArticleAmount);

        Line articleLine = lineList.get(position);

        articleName.setText(articleLine.getLineArticleDescription());
        articleAmount.setText(articleLine.getLineArticleAmount().toString());

        return parent;      
    }
}

Below is the LogCat log for clarification (to me it means nothing).

E/AndroidRuntime(15941): FATAL EXCEPTION: main
E/AndroidRuntime(15941): Process: be.mabolifting.maboserve, PID: 15941
E/AndroidRuntime(15941): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
E/AndroidRuntime(15941):    at android.widget.ListView.setupChild(ListView.java:1826)
E/AndroidRuntime(15941):    at android.widget.ListView.makeAndAddView(ListView.java:1793)
E/AndroidRuntime(15941):    at android.widget.ListView.fillDown(ListView.java:691)
E/AndroidRuntime(15941):    at android.widget.ListView.fillFromTop(ListView.java:752)
E/AndroidRuntime(15941):    at android.widget.ListView.layoutChildren(ListView.java:1630)
E/AndroidRuntime(15941):    at android.widget.AbsListView.onLayout(AbsListView.java:2087)
E/AndroidRuntime(15941):    at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941):    at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
E/AndroidRuntime(15941):    at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941):    at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941):    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
E/AndroidRuntime(15941):    at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941):    at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E/AndroidRuntime(15941):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E/AndroidRuntime(15941):    at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941):    at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
E/AndroidRuntime(15941):    at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941):    at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E/AndroidRuntime(15941):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E/AndroidRuntime(15941):    at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941):    at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
E/AndroidRuntime(15941):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
E/AndroidRuntime(15941):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
E/AndroidRuntime(15941):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
E/AndroidRuntime(15941):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
E/AndroidRuntime(15941):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
E/AndroidRuntime(15941):    at android.view.Choreographer.doFrame(Choreographer.java:544)
E/AndroidRuntime(15941):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
E/AndroidRuntime(15941):    at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(15941):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(15941):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(15941):    at android.app.ActivityThread.main(ActivityThread.java:5086)
E/AndroidRuntime(15941):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15941):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(15941):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(15941):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(15941):    at dalvik.system.NativeStart.main(Native Method)

Answer

laalto picture laalto · Nov 19, 2014

In your getView(), change

return parent;      

to

return rowView;

getView() should return the row view and not the parent where the rows are placed in. The parent view is only supplied as a paramteter so that view inflation can deal with match_parent sizes and such.