-2

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

  • why would you want that annotation there? what purpose would it serve? – Stultuske Oct 27 '17 at 06:03
  • check this it may help you https://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene – AskNilesh Oct 27 '17 at 06:04
  • 2
    move your `@Override` before `onCreate()` not before your variable `switchState` – dotGitignore Oct 27 '17 at 06:07
  • you also may want to re-think your logic. your Switched method, for instance. why don't you follow naming convention and call it switched instead of Switched? why do you pass a parameter you don't use? the code within can be reduced to: switchButton.setText(switchState ? "ON" : "OFF"); switchState = !switchState; – Stultuske Oct 27 '17 at 06:10
  • I tried that, the error was gone but my app still crashes. I think it's crashing for some other reason – ThatDanielFernandes Oct 27 '17 at 06:10

2 Answers2

0

As according @Jerrol you can place the @Override before onCreate. There is no need to place it before variable initialization.Could you give us the crash log that you are getting after removal.

Aditi
  • 389
  • 4
  • 13
0

try this

1.move you switchbutton = (Button) findViewById(R.id.Switch); inside your onCreate() method

2.place @Override before onCreate()

sample code

public class MainActivity extends AppCompatActivity {
    public boolean switchState = false;
    Button switchbutton ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        switchbutton = (Button) findViewById(R.id.Switch);
    }

    public void Switched(View v){

        if(!switchState){
            switchState = true;
            switchbutton.setText("OFF");
        }else{
            switchState = false;
            switchbutton.setText("ON");
        }
    }
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163