So this is my main layout and I want to add items programmatically as they will be dynamic.
<LinearLayout 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:id="@+id/mainlayout" >
</LinearLayout>
The problem is, when I look for the LinearLayout with;
LinearLayout layout = (LinearLayout)findViewById(R.id.mainlayout);
Immediately after super.onCreate(savedInstanceState); it returns null. Even if I wait until onStart() or onResume(), it doesn’t seem to be there. Any suggestions as to why this might be the case?
– Immediately after
super.onCreate(savedInstanceState)you should usesetContentView(R.layout.youLayout).– Its only after the
setContentView()method, one can assign ids to theViews, cause till then theViewsaren’t initialized.