Getting ClassCastException when trying to insert RelativeLayout dyanmically?

后端 未结 1 679
無奈伤痛
無奈伤痛 2020-12-11 01:56
        setContentView(R.layout.main2);

        out= new ArrayList();
        llay =(RelativeLayout) findViewById(R.id.displayPoll);

       Relativ         


        
相关标签:
1条回答
  • 2020-12-11 02:13

    The mistake here is in the choice of the subclass of LayoutParams.

    The correct version of LayoutParams to instantiate depends on the parent of the view you're creating, not the type of the view itself.

    Take a look at the logcat dump:

    04-06 20:30:49.372: E/AndroidRuntime(1093): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
    04-06 20:30:49.372: E/AndroidRuntime(1093):     at android.widget.ScrollView.onMeasure(ScrollView.java:301)
    

    Android is trying to typecast the instance of RelativeLayout.LayoutParams to, what I believe should be ScrollView.LayoutParams, because your RelativeLayout is inside of a ScrollView.

    The fix should be to change the layout params type that you're using on the RelativeLayout to ScrollView.LayoutParams.

    0 讨论(0)
提交回复
热议问题