Android 无法实例化类型View.OnClickListener Java错误

Android 无法实例化类型View.OnClickListener Java错误,android,syntax,syntax-error,Android,Syntax,Syntax Error,我正在创建一个Android应用程序,在我的最后一个问题()之后,我在Eclipse中遇到了以下语法错误: Cannot instantiate the type View.OnClickListener 我的代码如下: package com.example.ldsm3; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.DialogIn

我正在创建一个Android应用程序,在我的最后一个问题()之后,我在Eclipse中遇到了以下语法错误:

Cannot instantiate the type View.OnClickListener
我的代码如下:

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;
    }
}
我该如何解决这个问题?我知道这可能是一个简单的Java错误,但我以前没有使用过Java,请在提到Java语法、术语等时解释一下。

更改为

 public class Finished extends Activity implements View.OnClickListener
OnClickListener
是一个接口,您的类实现该接口

而且

 button1.setOnClickListener(this);
因为你已经有了

 public void onClick(View v) 
并确保您具有正确的导入

 import android.view.View.OnClickListener;
或者使用内部类。Annomymous内部类实现接口


尝试这样代替
button1.setOnClickListener(new Button.OnClickListener())

改变


并导入android.view.view.OnClickListener
,然后在
OnClickListener
OnClickListener
中执行任何您想执行的操作

这不是应该的工作方式
OnClickListener
是一个接口,因此必须在类中实现它,并将该类作为参数传递

您似乎已经在
活动中实现了该方法,因此:

  • 实现视图。OnClickListener
    添加到
    活动
    声明中
  • OnCLickListener
    设置为
    活动

    button1.setOnClickListener(this); 
    

我该怎么做?我不完全了解Java。请阅读一本关于Android编程的书,至少让自己了解一些基本知识。我对这个问题很好奇,不是因为我真的想实例化这些,而是因为我想了解更多关于Java中匿名内部类的知识。似乎OnClickListener类从未打算在setOnClickListener之外实例化。这些对象不是一等公民吗?
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);
        };
    });
button1.setOnClickListener(new Button.OnClickListener()); 
button1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {

    }
});
button1.setOnClickListener(this);