I am fairly new to android development and as my first app I want to make a flashlight app. So the first steps I want to do is to make a button in the center such that it changes it's text to "ON" and "OFF" alternately after every click. I made the java code but it Android studio gives me this error: '@Override' not applicable to field. Here is my java code:
package com.danielfernandzz.flashlight;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
public boolean switchState = false;
Button switchbutton = (Button) findViewById(R.id.Switch);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Switched(View v){
if(!switchState){
switchState = true;
switchbutton.setText("OFF");
}else{
switchState = false;
switchbutton.setText("ON");
}
}
}
And here is my xml code (in case you need it):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.danielfernandzz.flashlight.MainActivity">
<Button
android:id="@+id/Switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="8dp"
android:onClick="Switched"
android:text="ON"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I tried commenting out the @Override and no error came. But when I ran the app, it crashed so I assume that the app crashed because @Override wasn't there? I don't understand why this is happening to me, I have seen other questions but their error is something different. NOTE: I am using the latest version Android Studio 3.0