做个软件的登入注册界面,可是刚开始的一个登录就出现问题了,代码如下
public class Login extends Activity {
Button login, register, out;
private static EditText username, password;
Intent intent = null;
private ButtonListener listener = new ButtonListener(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.down); login = (Button) findViewById(R.id.btn_Login);
register = (Button) findViewById(R.id.btn_Reset);
out = (Button) findViewById(R.id.btn_Out); username = (EditText) findViewById(R.id.et_staff_no);
password = (EditText) findViewById(R.id.et_password); login.setOnClickListener(listener);
register.setOnClickListener(listener);
out.setOnClickListener(listener); } private final class ButtonListener implements OnClickListener { @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_Login:
Toast.makeText(Login.this, "登录了", 1).show();
intent = new Intent(Login.this, Down.class);
startActivity(intent);
break;
case R.id.btn_Reset:
Toast.makeText(Login.this, "注册了", 1).show();
break;
case R.id.btn_Out:
Toast.makeText(Login.this, "退出了", 1).show();
break;
}
} }我看了半天不知道哪里有问题,LOGCAT报错是login = (Button) findViewById(R.id.btn_Login);
register = (Button) findViewById(R.id.btn_Reset);
out = (Button) findViewById(R.id.btn_Out);
为空,可是我之前也写过这样的方法的,可以编译,但是现在怎么就不行了,实在找不出问题了,特来求教android 空指针异常

解决方案 »

  1.   

    肯定是佈局文件出錯了,你確定註冊界面的佈局文件是down.xml文件嗎?
      

  2.   

    认真查阅xml文件中的id,是否一样
      

  3.   

    intent = new Intent(Login.this, Down.class);Down在manifest里面注册了吗  这个是Activity  需要注册的
      

  4.   

    如果down中id是和getviewbyid中id是一样的建议修改下布局中的id有可能gen编译的时候出问题了。
      

  5.   

    只有 一种情况会出现这个问题,那就是你你引用了别的xml的id来这里显示了
      

  6.   

    setContentView(R.layout.down);  中的layout是不是包括你这些控件的layout,这种错误都是不仔细造成的
      

  7.   

    去R.java里看看有没有你定义的这几个button,没有的话应该是你的布局文件有问题,没能动态生成id,注意,也可能是别的xml文件的问题。