【问题标题】:Cannot instantiate the type View.OnClickListener Java error无法实例化类型 View.OnClickListener Java 错误
【发布时间】:2014-03-16 21:48:31
【问题描述】:

我正在创建一个 Android 应用程序,在我的最后一个问题 (Android app not able to open web link) 之后,我在 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 语法、术语等时解释一下。

【问题讨论】:

    标签: android syntax syntax-error


    【解决方案1】:

    改成

     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】:

      试试这样代替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);
              };
          });
      

      【讨论】:

      • 我对这个问题很好奇,不是因为我真的想实例化这些,而是​​因为我想更多地了解Java中的匿名内部类。似乎 OnClickListener 类从未打算在 setOnClickListener 之外实例化。 onClickListener 对象不是一等公民吗?
      【解决方案3】:

      改变

      button1.setOnClickListener(new Button.OnClickListener()); 
      

      button1.setOnClickListener(new OnClickListener()
      {
          @Override
          public void onClick(View v)
          {
      
          }
      });
      

      并导入android.view.View.OnClickListener,然后在onClickListeneronClick中做任何你想做的事情

      【讨论】:

        【解决方案4】:

        new Button.OnClickListener()

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

        看来您已经在Activity 中实现了该方法,所以:

        • implements View.OnClickListener 添加到您的Activity 声明中
        • OnCLickListener设置为Activity

          button1.setOnClickListener(this); 
          

        【讨论】:

        • 我该怎么做?我对 Java 不是很了解。
        • 请阅读一本关于 Android 编程的书,至少了解一些基本知识。
        猜你喜欢
        • 2022-01-19
        • 1970-01-01
        • 2023-02-10
        • 2013-09-28
        • 2015-02-28
        • 1970-01-01
        • 1970-01-01
        • 2013-02-15
        • 1970-01-01
        相关资源
        最近更新 更多