Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use addRule() with a tag dynamically inside a loop it takes Exception when inflating View?
    primarykey
    data
    text
    <p>I have a loop to add a variable number of views to a RelativeLayout, and I want to set some Rules like <code>BELOW</code>, my problem is that I'm using tags to name the objects dynamically, and to use the <code>addRule()</code> method I need to use an id, so what I tried is to set an Id every time that the loop is runned and it's not the first time.</p> <p>This is my code:</p> <pre><code>for(int i = 1; i &lt;= kNumberofViews; i++){ View = new MyView(context); RelativeLayout.LayoutParams params = new LayoutParams(kViewWidth, kViewHeight); params.setMargins(kMarginLeft, kMarginTop, kMarginRight, kMarginBottom); params.addRule(CENTER_HORIZONTAL); if (i != 1){ MyView view = (MyView)view.findViewWithTag(i-1); view.setId(i); params.addRule(RelativeLayout.BELOW, view.getId()); } View.setTag(i); View.setBackgroundColor(Color.GREEN); System.out.println("View " + i +" added correctly"); addView(View, params); } </code></pre> <p>How can I solve it?</p> <p>EDIT:</p> <pre><code>05-18 14:57:19.549: E/AndroidRuntime(5500): FATAL EXCEPTION: main 05-18 14:57:19.549: E/AndroidRuntime(5500): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.gabrielesteban.app/me.gabrielesteban.app.MyRelativeLayoutTest}: android.view.InflateException: Binary XML file line #7: Error inflating class me.gabrielesteban.app.GD.MyRelativeLayout 05-18 14:57:19.549: E/AndroidRuntime(5500): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 05-18 14:57:19.549: E/AndroidRuntime(5500): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357) 05-18 14:57:19.549: E/AndroidRuntime(5500): at android.app.ActivityThread.access$600(ActivityThread.java:153) </code></pre> <p>EDIT 2: </p> <p>I think that the problem is in the following line <code>MyView view = (MyView)view.findViewWithTag(i-1);</code>, is this line correct?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload