ruanjian502 2014-04-15 01:27 采纳率: 0%
浏览 4647

Android 在XML文件中控件监听,在Activity中实现不了,有异常IllegalStateException

  1. activity_main.xml

android:id="@+id/btLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
android:layout_alignParentRight="true"
android:onClick="login"
/>

  1. Activity代码

public class MainActivity extends Activity {
EditText etUserName = null;
EditText etPassword = null;
CheckBox cb = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    etUserName = (EditText) findViewById(R.id.etUserName);
    etPassword = (EditText) findViewById(R.id.etPassword);
    cb = (CheckBox) findViewById(R.id.cb);
}


public void login(View view){
    String userName = etUserName.getText().toString().trim();
    String password = etPassword.getText().toString().trim();
    if(TextUtils.isEmpty(userName)||TextUtils.isEmpty(password)){
        Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
    } else {
        //判断用户是否记住密码
        if(cb.isChecked()){
            //保存用户名密码
            Log.i("tag", "需要记住密码");
        }
        //登陆发送消息到服务器,服务器验证是否正确
        if("zhangsan".equals(userName)&&"123".equals(password)){
            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "登陆失败,用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

log中的不知道怎么复制上来----04-14 20:56:37.910: E/AndroidRuntime(1142): java.lang.IllegalStateException: Could not find a method login(View) in the activity class com.example.login.MainActivity for onClick handler on view class android.widget.Button with id 'btLogin'

  • 写回答

2条回答 默认 最新

  • hhblue 2014-05-23 12:40
    关注

    android1.6以上才能使用,并且只能给Button控件使用,好像是这样的。你检查看看,我看没什么错误。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog