当我将鼠标悬停在“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));
}
}
}
答案 0 :(得分:2)
您应该将代码移至onCreate
等方法中,以便设置onClickListener
。您不能在函数体外执行类似的代码。另外,在Button
之外宣布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));
}
}
答案 1 :(得分:2)
如果您想将original_data.attachments.forEach(function(i) {
var new_url = "https://example.com/" + i + ".png";
download(original_url, new_url, function(new_url){
console.log(new_url)
new_data.new_url = new_url;
save_new_url_to_mongodb(new_data);
});
})
放在外面,那么它应该是这样的:
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
答案 2 :(得分:0)
私人按钮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;
}
}