Я действительно не понимаю, почему этот простой код аварии в этой линии layout.addView(button);
Добавление кнопки в линейной компоновке врезается
super.onCreate(savedInstanceState);
layout = (LinearLayout)findViewById(R.id.linearLayout);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
button.setHeight(100);
button.setWidth(100);
button.setText("HELLO");
button.setLayoutParams(p);
layout.addView(button);
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout">
</LinearLayout>
EDIT: отредактированный код еще расправляется. Но теперь подавляет в setContentView.
super.onCreate(savedInstanceState);
layout = (LinearLayout)findViewById(R.id.linearLayout);
setContentView(layout);
LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
int i,j;
Button button = new Button(this);
button.setHeight(100);
button.setWidth(100);
button.setText("HELLO");
layout.addView(button,p);
Logcat отчет
вы должны вызвать setContentView ПЕРЕД f indViewById не ПОСЛЕ – Blackbelt
Тогда как я могу дать 'layout' как аргумент в setContentView? Я должен сначала получить его из xml, выполнив findViewById? – Tahlil
Вы не можете просто установить setContentView (R.layout.activity_main)? – Blackbelt