My Activity class looks like that:
public class FullscreenActivity extends AppCompatActivity {
private Button btnTakePhoto = new Button(this);
private Button btnRecordVideo = new Button(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
btnTakePhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
helloWorldKamera();
}
});
}
public void helloWorldKamera() {
System.out.println("Kamera");
}
}
My activity_fullscreen.xml
file looks like that:
<FrameLayout 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:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Foto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:id="@+id/btnTakePhoto" />
<Button
android:text="Video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:id="@+id/btnRecordVideo"
/>
</LinearLayout>
</FrameLayout>
Now I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
.
Do you know how to fix the null pointer exception? I don't know why my button is null.
Is it right how I initialize the button or is the error caused by findViewById
?