I have been trying to add relative layouts to a linear layout inside a scroll view in android-studio.
It kept giving me an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
I thought maybe I can't add it to the scroll view because of so many layouts. So I removed everything and created a small code like so:
public class ChildAccountsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child_accounts_activity);
LinearLayout linearLayout = findViewById(R.id.accountHolder);
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
linearLayout.addView(myButton); <<--ERROR HERE
}
Here I am just trying to add a button to the linear layout, but it keeps giving me the error mentioned above.
Any guidance will be appreciated and please keep in mind that I'm new to programming and very likely don't know what I'm doing. Thanks for your time :D