I am trying to instantiate and add a view object programmatically
RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
View playControlsPanelMinimized = new View(this);
activityMain.addView(playControlsPanelMinimized);
and I get this error in log cat
Attempt to invoke virtual method
'void android.widget.RelativeLayout.addView(android.view.View)'
on a null object reference
at com.example.michael.musicplayer.PlayPanel.onBackPressed(PlayPanel.java:50)
This is onBackPressed()
method and line # 50
@Override
public void onBackPressed() {
super.onBackPressed();
// some code
);
RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
View playControlsPanelMinimized = new View(this);
activityMain.addView(playControlsPanelMinimized); // Line # 50
}
This is activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".MainActivity"
android:id="@+id/activity_main">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<RelativeLayout
android:id="@+id/hidden_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listView1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</RelativeLayout>
</RelativeLayout>