Reputation: 1084
I'm creating an Android app and after my last question (Android app not able to open web link), I've been getting this syntax error in Eclipse:
Cannot instantiate the type View.OnClickListener
My code is as follows:
package com.example.ldsm3;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Finished extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finished);
// tv is the ID for the TextView in the XML file
TextView tv = (TextView) findViewById(R.id.textView2);
// set the TextView to show the score
tv.setText(Misc.correct + "/" + Misc.total);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener());
}
public void onClick(View v)
{
// Open up the system's default browser to whackamole.ajav-games.tk, where the Whack A Mole game is.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://whackamole.ajav-games.tk"));
startActivity(browserIntent);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.finished, menu);
return true;
}
}
How do I fix this? I know it is probably a simple Java error, but I have not used Java before, and please, when referring to Java syntax, terms, etc. explain them.
Upvotes: 3
Views: 6638
Reputation: 133580
Change to
public class Finished extends Activity implements View.OnClickListener
OnClickListener
is a interface and your class implements the interface
And have
button1.setOnClickListener(this);
because you already have
public void onClick(View v)
and make sure you have the right import
import android.view.View.OnClickListener;
OR using inner class. Annomymous inner class implements the interface
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://whackamole.ajav-games.tk"));
startActivity(browserIntent);
}
});
Upvotes: 2
Reputation: 12919
new Button.OnClickListener()
is not how this is supposed to work. OnClickListener
is an interface, so you have to implement it in a class and pass that class as the argument.
It seems you already implemented the method in your Activity
, so:
implements View.OnClickListener
to your Activity
declarationSet the OnCLickListener
to the Activity
:
button1.setOnClickListener(this);
Upvotes: 2
Reputation: 13520
Change
button1.setOnClickListener(new Button.OnClickListener());
to
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
and import android.view.View.OnClickListener
then do whatever you want to do in onClick
of the onClickListener
Upvotes: 3
Reputation: 21561
Try like this in place of button1.setOnClickListener(new Button.OnClickListener());
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO your code
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://whackamole.ajav-games.tk"));
startActivity(browserIntent);
};
});
Upvotes: 0