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"
tools:context=".GameActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:id="@+id/container">
</FrameLayout>
</RelativeLayout>
GameActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Field field = new Field(this);
}
Field:
public class Field extends View {
private Context context;
public Field(Context context) {
super(context);
this.context = context;
createButton();
}
public void createButton()
{
FrameLayout container = (FrameLayout) findViewById(R.id.container);
Button b = new Button(context);
b.setText("123");
b.setHeight(25);
container.addView(b); //NullPointerrException
}
}
Что я делаю не так?