我正在阅读一本 java 练习书,并陷入了这个语法错误。我已经搜索了 onSetClickListener 语法错误并且不认为我在重复问题。Eclipse 说“令牌“setOnClickListener”上的语法错误,此令牌后应有标识符”。谢谢你的帮助!
package fifth.example.eventhandling;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this); <<ERROR HERE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View arg0) {
TextView text = (TextView)findViewById(R.id.textmessage);
text.setText("BUTTON HAS BEEN CLICKED. EVENT PROCESSED.");
}
}