当我将鼠标悬停在“ setOnClickListener”下的波浪状红色线条上时,会弹出消息,提示“无法解析符号setOnClickListener”,“ @ Override”说“此处不允许注释”。而且“查看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
进行设置。您不能在函数体内执行类似的代码。另外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));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句