Unable to instantiate Java View.OnClickListener error

I am creating an Android application and after my last question ( Android application cannot open the web link ), I get 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 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 to fix it? I know that this is probably a simple Java error, but I have not used Java before, and please explain them by reference to Java syntax, terms, etc.

+3
source share
4 answers

Try this instead 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);
        };
    });
0
source

Edit

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 inonClick onClickListener

+3
source

 public class Finished extends Activity implements View.OnClickListener

OnClickListener - ,

 button1.setOnClickListener(this);

 public void onClick(View v) 

,

 import android.view.View.OnClickListener;

.

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);
 }
}); 
+2
source

new Button.OnClickListener()

not how it should work. OnClickListeneris an interface, so you need to implement it in a class and pass this class as an argument.

It seems that you have already implemented the method in your own Activity, therefore:

  • Add implements View.OnClickListenerto your adActivity
  • Set OnClickListenerto Activity:

    button1.setOnClickListener(this); 
    
+2
source

All Articles