我創建一個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語法,術語等時解釋它們。
我對這個問題很好奇,並不是因爲我真的想實例化這些問題,b因爲我想了解更多關於Java中匿名內部類的知識。看起來OnClickListener類從來不打算在setOnClickListener之外實例化。 onClickListener對象不是一流的公民嗎? –