I am new to Android and tried to run the sample code of the SDK using eclipse. I created the project (via File → New → Project → Android → Android Sample Project), but when I run the project it shows the following error for all the sample projects:
Multiple markers at this line
The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
implements android.view.View.OnClickListener.onClick
Then I refered to this forum. Here they mentioned a "compiler compliance level". But my compiler compliance level is on 1.7, and I am using Android 4.0.3 with an API level of 15.
So please help me to rectify this error.
Make sure that above your OnClick method you have the annotation #Override like so
#Override
public void OnClick(View v) {
//Code goes here
}
By having #Override, Java knows that you are not creating a new method, in this situation you are letting Java know that you are utilizing that listener.
If you have already done this, check this website http://androidcodemonkey.blogspot.com/2011/10/how-to-solve-must-override-superclass.html
Remove the #Override annotation tag.
For some situation. the eclipse can't recognize.
I just had the same issue.I fixed it by following change of code
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Use View.OnClickListener(){} instead of just OnClickListener(){};
I think this might due to various OnClickListener are there. May be we need to specify which is the relevant one. I would really love to know the real reason if any one knows.
Related
When I add this line of code in my Activity
findViewById(R.id.btFilter).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
ResultadoBuscaMapa.this.finish();
return true;
}
});
I cannot compile, and get this error
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.util.ExceptionWithContext
at com.android.dx.util.ExceptionWithContext.withContext(ExceptionWithContext.java:46)
...
in both Intellij IDEA and Android Studio (latest versions)
Funny thing is that it's an old code and worked just fine until yesterday.
Is it possible that Android Studio broke something in my project structure?
Where do you declare this listener ? It's better to do ContextView.findViewById(R.id.btFilter);
And i usually declare my widget before setting a listener:
Button filter = (Button) findViewById(R.id.btFilter);
filter.setOnLongClickListener(....
Another way, do you try without ResultadoBuscaMapa.this.finish(); cause error seems to be link to your context.
Hope it helps
Strange as it my seem, after the last update of Android Studio, the error just vanished in BOTH IDE's. Now it runs fine also in Intellij IDEA
In some case, when accessing an activity field from an anonymous class, I get NoSuchFieldError at runtime: java.lang.NoSuchFieldError: MyActivity.myField
EDIT: I'm now sure that it's an Android issue, because everything compiles correctly but resolved incorrectly at runtime. When changing to MyActivity.this.myField instead of plain myField, everything works perfect.
BTW, I rechecked and I don't have any other myField anywhere else, Also, when opening declaration in Eclipse, it goes to the intended field (myField).
The only problem is that I couldn't reproduce this in a sample project. I'm left only to state that it's a specific problem in my project, but I'm reluctant doing so and I can't publish this project.
I'll keep this open in case someone stumbles upon something similar.
Here's the code that I can publish that crashes my project but not the sample one:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Runnable() {
#Override
public void run() {
Toast.makeText(MyActivity.this, myField.getName(), Toast.LENGTH_SHORT).show();
}
}.run();
}
It seems that accessing the field with the class' prefix solves it: MyActivity.this.myField
Strange. I guess it's something with Android's field resolution in runtime.
With this code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
. . .
Button buttonAuthorizeUsers = (Button) findViewById(R.id.buttonAuthorizeUsers);
buttonAuthorizeUsers.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent configure = new Intent(OnDemandAndAutomatic_Activity.this, Configure_Activity.class);
OnDemandAndAutomatic_Activity.this.startActivity(configure);
}
});
I'm getting:
The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
It would seem that this problem is sometimes caused by a Project | Properties | Java Compiler being set to 1.5.
Although I'm virtually I'd had this problem before, and changed it to 1.6, somehow it WAS 1.5 again.
HOWEVER, that (changing it to 1.6) did not solve the problem. I'm still getting that same err msg, after cleaning, building, and F11ing...???
I would recommend that you uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above. Otherwise you would have to specify it every time.
If you need a specific compliance level for a specific project, you need to verify every other project that need compliance level 1.6 or above is set to this.
After everything is correctly setup - clean projects and restart Eclipse. Eclipse can be such a bitch some times - this often solves problems for me.
Two things to consider:
1) Take a look at your imports - are you sure that View.OnClickListener is imported, but not lets say DialogInterface.OnClickListener
2) OnClickListener is actually an interface, that you are instantiating anonymously. So after all when writing the onClick method you are actually not overriding a super class method, but instead implementing an interface method. Annotating interface methods with #Override is a good practice, but this has been introduced in JDK 6, which means that by the time Android 1.5 or 1.6 was developed this may not has been yet introduced to the java language and hence making it an invalid syntax.
Right below the "Compiler Compliance Level", there are a few options grayed out if the "Use default compliance settings" checkbox is checked: Namely, "Generated .class files compatibility" and "Source compatibility". Verify that both of those are set to 1.6 - If not, either change the default compliance settings, or uncheck that box and tweak them directly.
Button buttonAuthorizeUsers = (Button) findViewById(R.id.buttonAuthorizeUsers);
buttonAuthorizeUsers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent configure = new Intent(OnDemandAndAutomatic_Activity.this,Configure_Activity.class);
OnDemandAndAutomatic_Activity.this.startActivity(configure);
}
});
try to replace this line
buttonAuthorizeUsers.setOnClickListener(new View.OnClickListener() {});
this error you got happened when your trying to assignee the On-click to unexpected type !
So, beleive me Eclipse IDE most of time will import DialogInterface instead of View so write it by your self.
daigoor is right. Eclipse always try to do this 'import android.content.DialogInterface.OnClickListener' instead of the doing this -> 'import android.view.View.OnClickListener'. That solves my problem.
Using Eclipse's Shift+Ctrl+O to organize imports has been working fine until it stopped working.
For some reason, it is now taking away my necessary imports and causing classes to be unresolved. Example below, Button, onClick, Toast are not resolved due to when Shift-Ctrl-O was pressed, it cleaned out the needed file.
It was working before and I don't know what I did to cause it to not work anymore. Any help would be greatly appreciated. I cleaned project, restart eclipse...to no avail.
final Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Perform action on clicks
Toast.makeText(HelloFormStuffActivity.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});
Thanks very much in advance.
I have/had the same problem. Best I could figure out is that eclipse need to have a 'source' item on the main tool bar which has has 'options' like format and 'Organize Imports'. Clicking on a project in the "Package Explorer" causes the 'source' menu item to appear between 'refactor' and 'run'.
I had this happen when I created a file without a .java extension and renamed it with Refactor. Not until I noticed there was no syntax highlighting did I realize I still had it open in the text editor, so I closed it and reopened and Ctrl+Shift+O started working again.
I can get the following to compile (by commenting out the "#Override"):
Button buttonAuthorizeUsers = (Button) findViewById(R.id.buttonAuthorizeUsers);
buttonAuthorizeUsers.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
Intent configure = new Intent(OnDemandAndAutomatic_Activity.this, Configure_Activity.class);
OnDemandAndAutomatic_Activity.this.startActivity(configure);
}
});
...but it causes my app to crash. When I comment out it runs fine (but I have no button handler).
Should the class have a "implements OnClickListener"? It seems like I should, but it compiles without it, and won't compile WITH it (whether I comment out the "override" or not, it's unhappy).
as per my suggestion, it is not good to remove #Override line. you need to change to JDK version in your eclipse then you will not get such errors. Follow, following steps for it,
Right Click on your Project & select Properties.
Then from the left section Select Java Compiler & select the Version of the JDK you installed. If it is already selected then de-select it & try it.
Dont remove #Override. just replace
setOnClickListener(new OnClickListener() {
with setOnClickListener(new View.OnClickListener() {
and also replace implements OnClickListener with implements View.OnClickListener in your class declaration line.
save and then compile . your problem will get solved.