-5

我正在阅读一本 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.");

    }

}
4

3 回答 3

3

一个问题是:您在代码中的按钮相关代码应该在里面onCreate()和之后setContentView...

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

应该:

public class MainActivity extends Activity implements OnClickListener{ Button button;//声明按钮听

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button1);
       button.setOnClickListener(this);  <<ERROR HERE

    }

}

于 2013-01-22T04:49:57.040 回答
2

目前,您正在尝试在为 Activity 设置布局之前初始化按钮。在 as之后在 Activity 的 onCreate 中移动按钮初始化 setContentView

public class MainActivity extends Activity implements OnClickListener{
    Button button ; //<<< declare  button here

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       button = (Button)findViewById(R.id.button1);  //<<< initilze here
       button.setOnClickListener(this);   
    }
于 2013-01-22T04:53:12.003 回答
0

你的代码应该是这样的。

public class MainActivity extends Activity implements OnClickListener{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);  <<ERROR HERE
}

@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.");

}
于 2013-01-22T04:55:42.610 回答