Hellz Yeahh Hellz Yeahh - 6 months ago 84
Java Question

The (View v) in Android studio is always "Cannot Resolve", and red no matter what

I've tried everything even re-syncing and reinstalling Android studio. The onClick(View v) the "v" is always red and it says Cannot resolve symbol. I've imported view.OnclickListener too and View.view, and still the same thing. I can't even compile the code, or even build the APK.
here is the code:

package com.example.alexm.calc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

EditText percentedit = (EditText) findViewById(R.id.percentedit);
EditText valueedit = (EditText) findViewById(R.id.valueedit);
Button button = (Button) findViewById(R.id.button);
TextView result = (TextView) findViewById(R.id.result);

button.setOnClickListener(new View.OnClickListener());{
public void onClick(View v){

}
}


}
}


and here is the error log:

Error:(24, 13) error: illegal start of expression
Error:(24, 20) error: illegal start of expression
Error:(24, 32) error: ';' expected
Error:(24, 39) error: ';' expected
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.


Notice even when I put ';" semicolon it will say "Unnecessary ";""
it is bugging me so much. Plese help

Answer

You've got a semicolon in there that you don't need.

button.setOnClickListener(new View.OnClickListener());{

Should look like

button.setOnClickListener(new View.OnClickListener() { 
    // onClick... 
});
Comments