addView to layout with java
<p>I am trying to understand how to add views programatically (with java) in Android applications. I created a simple layout (below) to practice that but I struggle to solve the following error:</p>

<p><em>java.lang.IllegalStateException: Could not find method addingView() in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'</em></p>

<p><a href="https://i.stack.imgur.com/VymyA.png" rel="nofollow noreferrer">Screenshot from the app:</a></p>

<p>My XML:</p>

<pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.view_create_test_1.MainActivity"&gt;

    &lt;LinearLayout
        android:id="@+id/top"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:background="#555555"&gt;
    &lt;/LinearLayout&gt;

    &lt;RelativeLayout
        android:id="@+id/bottom"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#116677"&gt;

        &lt;Button
            android:text="Add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:layout_centerInParent="true"
            android:onClick="addingView"/&gt;
    &lt;/RelativeLayout&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>My JAVA:</p>

<pre><code>public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void addingView (){
        LinearLayout top = (LinearLayout) findViewById(R.id.top);
        LinearLayout newLayout = new LinearLayout (this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        newLayout.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, 50));
        top.addView(newLayout,params);
    }

}
</code></pre>

<p>Please help me out how to solve the error. Thanks</p>