我想在我的代码中使用findViewById,但它必须是无效的,什么是错的?我不想要方法clickbutton,但如果我把它拿出来,它说它一定是无效的。有什么办法可以在不改变代码中的所有内容的情况下做到这一点?findViewById必须是无效的,但不能使其无效
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class LoginWorker extends Activity implements OnClickListener{
Button login;
EditText user, pass;
ToggleButton repass;
TextView invail;
private boolean savelogin = false;
public void clickbutton() {
login = (Button) findViewById(R.id.bloginf);
user = (EditText) findViewById(R.id.usernametf);
pass = (EditText) findViewById(R.id.passwordtf);
invail = (TextView) findViewById(R.id.invalidinfo);
repass = (ToggleButton) findViewById(R.id.rememberlogintb);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(user.getText().equals("parents")){
if(pass.getText().equals("!saints")){
if(repass.isChecked()){
savelogin = true;
setContentView(R.layout.announcements);
}
}else if(!repass.isChecked()){
savelogin = false;
setContentView(R.layout.announcements);
}
}if(!user.getText().equals("pie")){
invail.setText("Invalid Username and/or Password!");
}if(!pass.getText().equals("!pie")){
invail.setText("Invalid Username and/or Password!");
}
}
});
}
}
谢谢你的时间!
我建议你按照Android上的初学者教程:[链接](http://www.codeproject.com/Articles/102065/Android-A-beginner-s-guide)。 – Vikram