Java 我的setOnClickListener是否给了我一个错误?

Java 我的setOnClickListener是否给了我一个错误?,java,android,android-studio,button,Java,Android,Android Studio,Button,当我将鼠标悬停在“setOnClickListener”下的扭曲红线上时,会弹出一条消息,上面写着“无法解析符号setOnClickListener”,“@Override”表示“此处不允许使用批注”。来自“View v”的v也给了我一个错误。我在哪里搞砸了 import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android

当我将鼠标悬停在“setOnClickListener”下的扭曲红线上时,会弹出一条消息,上面写着“无法解析符号setOnClickListener”,“@Override”表示“此处不允许使用批注”。来自“View v”的v也给了我一个错误。我在哪里搞砸了

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class EmailReceiptActivity extends AppCompatActivity {


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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_options_menu, menu);
    menu.findItem(R.menu.main_options_menu).setIntent(
            new Intent(EmailReceiptActivity.this, LaunchActivity.class));
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    //use the id of the item from main_options_menu
    if (id == R.id.logoff_menu_item) {
        super.onOptionsItemSelected(item);
        startActivity(item.getIntent());
    }

    return true;
}

Button btn_send = (Button)findViewById(R.id.send_receipt_button);
btn_send.setOnClickListener(new View.OnClickListener(){
    @Override
    //Use the name of the function you assigned to the xml design of the button
    public void onClick(View v){
        //Use the name of this class, and the name class where you want to be taken when the button is clicked.
        startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class));
    }
}

}

您应该将代码移动到诸如
onCreate
之类的方法中,以便设置
onClickListener
。您不能在函数体之外执行这样的代码。另外,在
onCreate
之外,用更全局的范围声明
按钮可能更有用,所以请尝试

public Button btn_send;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_email_receipt);

    btn_send = (Button)findViewById(R.id.send_receipt_button);
    btn_send.setOnClickListener(new View.OnClickListener(){
        @Override
        //Use the name of the function you assigned to the xml design of the   button
        public void onClick(View v){
            //Use the name of this class, and the name class where you want to be taken when the button is clicked.
            startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class));
    }
}

专用按钮发送\接收\按钮

public class GameOver extends AppCompatActivity implements View.OnClickListener {

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_email_receipt);
              Button btn_send = (ImageButton) findViewById(R.id.send_receipt_button);
             btn_send.setOnClickListener(this);

}

  public void onClick (View v){



            switch (v.getId()) {

                case R.id.send_receipt_button:
                     startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class));


                    break;
}
}

如果你想把你的
OnClickListener
像这样放在外面,那么它应该是这样的:

OnClickListener sendListener = new View.OnClickListener(){
    @Override
    //Use the name of the function you assigned to the xml design of the button
    public void onClick(View v){
        //Use the name of this class, and the name class where you want to be taken when the button is clicked.
        startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class));
    }
};
然后在类似于
onCreate

Button btn_send;
protected void onCreate(Bundle savedInstanceState) {
    ...

    btn_send.setOnClickListener(sendListener);
}
Button btn_send;
protected void onCreate(Bundle savedInstanceState) {
    ...

    btn_send = (Button)findViewById(R.id.send_receipt_button);
    btn_send.setOnClickListener(new View.OnClickListener(){
        @Override
        //Use the name of the function you assigned to the xml design of the   button
        public void onClick(View v){
            //Use the name of this class, and the name class where you want to be taken when the button is clicked.
            startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class));
    }
}
但理想情况下,您希望将
findviewbyd
OnClickListener
移动到类似
onCreate

Button btn_send;
protected void onCreate(Bundle savedInstanceState) {
    ...

    btn_send.setOnClickListener(sendListener);
}
Button btn_send;
protected void onCreate(Bundle savedInstanceState) {
    ...

    btn_send = (Button)findViewById(R.id.send_receipt_button);
    btn_send.setOnClickListener(new View.OnClickListener(){
        @Override
        //Use the name of the function you assigned to the xml design of the   button
        public void onClick(View v){
            //Use the name of this class, and the name class where you want to be taken when the button is clicked.
            startActivity (new Intent(EmailReceiptActivity.this, SuccessActivity.class));
    }
}

这个答案看起来像是原始内容,但你应该知道,我在谷歌上搜索了一点你在这里给出的答案()删除它是正确的做法,因为它是从这里复制的,没有属性:。这被认为是剽窃,在这里很不受欢迎,而且很容易被发现(正如你所见)。这是我在应用程序中使用的原始代码。并显示存在的答案是帮助和不剽窃,剽窃不是在一个博客或服务维基上发布的东西。我相信你的动机是帮助别人,这是很感激的。向某人展示另一个人答案的正确方式是链接到该答案。如果读者复制部分内容更有意义,那么就引用来源。只向用户展示最佳答案才有意义。我只是想帮忙,这就解决了。非常感谢你!