2014-02-17 117 views
3

我創建一個Android應用程序,我的最後一個問題(Android app not able to open web link)後,我已經在Eclipse中得到這個語法錯誤:無法實例類型View.OnClickListener Java錯誤

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語法,術語等時解釋它們。

回答

0

嘗試這樣到位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

我對這個問題很好奇,並不是因爲我真的想實例化這些問題,b因爲我想了解更多關於Java中匿名內部類的知識。看起來OnClickListener類從來不打算在setOnClickListener之外實例化。 onClickListener對象不是一流的公民嗎? –

2

更改爲

public class Finished extends Activity implements View.OnClickListener 

OnClickListener是接口和類實現接口

而且具有

button1.setOnClickListener(this); 

,因爲你已經有

public void onClick(View v) 

並確保您有正確的進口

import android.view.View.OnClickListener; 

或使用內部類。 Annomymous內部類實現接口

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); 
} 
}); 
3

變化

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

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

    } 
}); 

,並導入android.view.View.OnClickListener然後做任何你想做的的onClickListener

2

new Button.OnClickListener()

onClick做的是,這是不應該如何工作。 OnClickListener是一個接口,所以您必須在類中實現它並將該類作爲參數傳遞。

看來你已經在你的Activity實施的方法,所以:

  • 添加implements View.OnClickListenerActivity聲明
  • 設置OnCLickListenerActivity

    button1.setOnClickListener(this); 
    
+0

我該怎麼做?我不完全瞭解Java。 – javathunderman

+0

請閱讀關於Android編程的書籍,以獲得至少基本的知識。 –