在我的應用程序中,我需要顯示很多按鈕的對話框。因此,我決定使用1個onClick來製作一系列按鈕。只有我們執行的第一行,有一個錯誤。我的代碼如下:點擊查看時出錯
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.View;
public class Trial extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View b1 = findViewById(R.id.button1);
b1.setOnClickListener(this);
View b2 = findViewById(R.id.button2);
b1.setOnClickListener(this);
}
View.OnClickListener yourListener = new View.OnClickListener() {
public void onClick(View v) {
if (v == button1) {
new AlertDialog.Builder(v.getContext())
.setTitle("Paracettamol")
.setMessage(
"This medicine is generally used to cure Fever")
.setNeutralButton("OK", null).show();
} else if (v == button2) {
new AlertDialog.Builder(v.getContext())
.setTitle("sertraline")
.setMessage(
"This medicine is generally used to cure Head aches")
.setNeutralButton("OK", null).show();
}
}
}
第五行(公共類試行擴展活動實現View.OnClickListener)如下給出了一個錯誤:類型試驗必須實現繼承的抽象方法View.OnClickListener.onClick(查看) 。誰能幫幫我嗎。
哇,你應該檢查你的代碼縮進! :)您可以通過預先添加四個空格來縮進一行。 – Matthias 2011-06-06 14:14:58