【问题标题】:My is my setOnClickListener giving me an error? [duplicate]我的 setOnClickListener 给我一个错误吗? [复制]
【发布时间】:2017-01-18 15:57:47
【问题描述】:

当我将鼠标悬停在“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));
    }
}

}

【问题讨论】:

    标签: java android android-studio button


    【解决方案1】:

    您应该将代码移至 onCreate 等方法中,以便设置 onClickListener。你不能在函数体之外执行这样的代码。此外,在onCreate 之外使用更全局的范围声明您的Button 可能更有用,所以请尝试...

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

    【讨论】:

    • 这解决了它。非常感谢!
    【解决方案2】:

    如果你想把你的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);
    }
    

    但理想情况下,您希望将您的 findViewById 和您的 OnClickListener 移动到像 onCreate 这样的区域

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

    【讨论】:

      【解决方案3】:

      私人按钮 send_receipt_button;

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

      【讨论】:

      • 这个答案看起来像原始内容,但你应该知道我在谷歌上搜索了你在这里给出的答案(stackoverflow.com/questions/39429490/…)删除它是正确的做法,因为它是在没有复制的情况下复制的来自这里的归属:stackoverflow.com/a/25081954/294949。这被认为是抄袭,在这里非常不受欢迎,而且很容易(如您所见)发现。
      • 这是我在我的应用程序上使用的原始代码。并且显示存在的答案是为了帮助而不是剽窃,剽窃不是在一个博客或服务维基上发布的东西。思考回答
      • 我确信您的动机是帮助他人,这是值得赞赏的。向某人展示另一个人的答案的正确方法是 link 到它。如果读者复制其中的一部分更有意义,请注明出处。
      • 只为用户显示最佳答案是有意义的。只想帮忙。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多