0

Could somebody tell me if there is anything wrong in the program shown below?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int addAToB(int a, int b)
    {  
        int answer = a + b;  
        return answer; 
    } 
}

I am a complete beginner in Android App Development and am facing this problem for quite some time . Whenever I make any method Android Studio is showing errors. Another example is this one.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(this, "Can you see me", Toast.LENGTH_SHORT).show();
    public void topClick (View v){
        Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();

    }
    public void bottomClick (View v){
        Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();
    }
}

It's an app in which two buttons are created and whenever the user clicks the top button a message of "Top button clicked" is shown and whenever the user clicks the button at the bottom a message of "Bottom button clicked" is shown.

I have reinstalled Android Studio twice in the hope the it might solve the issue but it didn't.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Raunak Deb
  • 51
  • 1
  • 10

3 Answers3

3

You are placing a void inside a void, which should not be like that in Java.

Try this structure:

protected void onCreate (Bundle savedInstanceState) {
    //some code
}

public void topClick(View v) {
    //some code
}

public void bottomClick(View v) {
    //some code
}

See this Stackoverflow question, you cannot place a method inside a method in Java.

Edit: Sorry didn't see someone else already was faster. If the other answer is accepted I'll delete this.

BenjyTec
  • 1,719
  • 2
  • 12
  • 22
2

Move your topClick and bottomClick functions outside. You are not supposed to declare a named function inside another function.

MrFisherman
  • 720
  • 1
  • 7
  • 27
DemiDust
  • 313
  • 1
  • 3
  • 19
0

You cannot declare method inside another method:

Change your code to this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
int addAToB(int a, int b)
    {  
        int answer = a + b;  
        return answer; 
    }

Also for the other code, you initialized button click event inside the method, which is wrong. Make changes and your code should look like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(this, "Can you see me", Toast.LENGTH_SHORT).show();

}

public void topClick (View v){
        Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();

    }
    public void bottomClick (View v){
        Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();


    }

This will work perfectly fine. Good Luck!

Prateek Aggarwal
  • 166
  • 1
  • 2
  • 14