嗨我有这个错误java.lang.NullPointerException com.ampo.mobileampo.ampo.MainActivity $ 1.onClick(MainActivity.java:53)

时间:2015-01-29 05:37:27

标签: java android

这是我的主要活动..所有我想要发生的是当用户点击登录用户将被定向到活动用户所属..如果用户名等于admin用户将指向管理员模块,如果用户不是等于管理员用户将直接到客户端模块..但每次我输入用户名和密码fiel后点击登录...我的模拟器总是停止..

 public class MainActivity extends ActionBarActivity {

Button btnLogin;
Button btnRegister;
EditText pass,un;
User userdb;

public String user_name;
public String pass_word;
private DBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    un = (EditText) findViewById(R.id.eTlogUser);
    pass = (EditText) findViewById(R.id.eTlogPass);



    btnLogin = (Button)findViewById(R.id.btnlogin);
    btnRegister = (Button) findViewById(R.id.btnregister);

    btnLogin.setOnClickListener(new View.OnClickListener(){

         /*  @Override
          public void onClick(View v) {
                  startActivity(new Intent(MainActivity.this,AdminModule.class));
                 }
              }); */


        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.btnlogin) {
                // dbHelper.onOpen(User.TABLE);
                Cursor cursor = dbHelper.getReadableDatabase().rawQuery("SELECT * FROM " + User.TABLE, null);
                cursor.moveToFirst();
                {
                    System.out.println("compiler was here!");

                    if (cursor.getCount() == 0) {
                        Toast.makeText(getApplicationContext(), "The table " + User.TABLE + " is empty", Toast.LENGTH_LONG).show();
                    } else if (cursor.getCount() >= 1) {

                        do {
                            if ((cursor.getString(9) == un.toString()) && (cursor.getString(10) == pass.toString())) {
                                Toast.makeText(getApplicationContext(), cursor.getString(1), Toast.LENGTH_LONG).show();
                                // Intent goToNextActivity = new Intent(getApplicationContext(), AdminModule.class);
                                if (cursor.getString(1) == "admin") {
                                    startActivity(new Intent(MainActivity.this, AdminModule.class));
                                } else {
                                    startActivity(new Intent(MainActivity.this, ClientModule.class));
                                }
                                // startActivity(goToNextActivity);

                                //startActivity(new Intent(MainActivity.this, AdminModule.class));
                            } else {
                                Toast.makeText(getApplicationContext(), "Wrong username/password", Toast.LENGTH_LONG).show();
                            }
                        } while (cursor.moveToNext());

                    }
                }
            }


        }

    });

    btnRegister.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {


            startActivity(new Intent(MainActivity.this,UserDetail.class));

         }

          });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
 }

这不是我在logcat中的错误...

01-29 00:28:08.148      887-887/com.ampo.mobileampo.ampo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ampo.mobileampo.ampo, PID: 887
    java.lang.NullPointerException
            at com.ampo.mobileampo.ampo.MainActivity$1.onClick(MainActivity.java:53)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

这是我的用户

中的crud操作代码
 public class UserDetail extends Activity implements View.OnClickListener {

Button ubtnsave, ubtnclose, ubtndelete;
EditText etutype, etusex,etusername;
EditText etucompany, etumail, etucont, etuadd;
EditText etloguserName, etlogpass, etlogconfirm;
private String pasword,pasword2;
private int _User_Id=0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_detail);

    ubtnsave = (Button) findViewById(R.id.ubtnsave);
    ubtnclose = (Button) findViewById(R.id.ubtnclose);
    ubtndelete = (Button) findViewById(R.id.ubtndelete);

    etusername= (EditText) findViewById(R.id.eTuname);
    etutype = (EditText) findViewById(R.id.eTutype);
    etusex = (EditText) findViewById(R.id.eTsex);
    etucompany = (EditText) findViewById(R.id.eTucompany);
    etumail = (EditText) findViewById(R.id.eTuemail);
    etucont = (EditText) findViewById(R.id.eTucont);
    etuadd = (EditText) findViewById(R.id.eTuadd);
    etloguserName = (EditText) findViewById(R.id.eTloguserName);
    etlogpass  = (EditText) findViewById(R.id.eTlogPass);
    etlogconfirm  = (EditText) findViewById(R.id.eTconfirmPass);

    _User_Id=0;
    Intent intentu = getIntent();
    _User_Id = intentu.getIntExtra("user_Id", 0);
    UserCrud uCrud = new UserCrud(this);
    User user = new User();
    user = uCrud.getUserById(_User_Id);

    etusername.setText(user.username);
    etutype.setText(user.usertype);
    etusex.setText(user.sex);
    etucompany.setText(user.usercompany);
    etumail.setText(user.usermail);
    etucont.setText(String.valueOf(user.usercontact));
    etuadd.setText(user.useradd);
    etloguserName.setText(user.loguser);
    etlogpass.setText(user.logpass);
    etlogconfirm.setText(user.logconfirmm);

}
        // stopped here 3:15 am

@Override
public void onClick(View v) {
    if (v == findViewById(R.id.ubtnsave)){
        Toast.makeText(this, pasword, Toast.LENGTH_LONG).show();
        Toast.makeText(this, pasword2, Toast.LENGTH_LONG).show();
        UserCrud uCrud = new UserCrud(this);
        User user = new User();
        user.privl = "Client";
        user.username=etusername.getText().toString();
        user.usertype=etutype.getText().toString();
        user.sex=etusex.getText().toString();
        user.usercompany=etucompany.getText().toString();
        user.usermail=etumail.getText().toString();
        user.usercontact=etucont.getText().toString();
        user.useradd=etuadd.getText().toString();
        user.loguser=etloguserName.getText().toString();
        user.logpass=etlogpass.getText().toString();
        user.logconfirmm=etlogconfirm.getText().toString();
        pasword  = etlogpass.getText().toString();
        pasword2 = etlogconfirm.getText().toString();

        if (_User_Id==0){
            if(pasword.equals(pasword2))
            {    _User_Id = uCrud.insert(user);
                Toast.makeText(this, "User Saved", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "Password doesn't Match" , Toast.LENGTH_LONG).show();
            }
        }else{
            uCrud.update(user);
            Toast.makeText(this, "User Updated", Toast.LENGTH_SHORT).show();
        }

    }else if (v == findViewById(R.id.ubtndelete)){
        UserCrud uCrud = new UserCrud(this);
        uCrud.delete(_User_Id);

        Toast.makeText(this, "User Deleted", Toast.LENGTH_SHORT).show();
        finish();
    }else if (v == findViewById(R.id.ubtnclose)){
        finish();
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_user_detail, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

 }

我不知道为什么我点击登录按钮后我的模拟器总是停止?

3 个答案:

答案 0 :(得分:0)

在主活动中,您未初始化以下dbhelper类对象

private DBHelper dbHelper;

初始化上面的Object Like:

 in OnCreate()

 dbHelper=new DBHelper(this);

答案 1 :(得分:0)

在活动的onCreate方法中初始化dbHelper对象

dbHelper=new DBHelper(MainActivity.this);

答案 2 :(得分:0)

初始化dbHelper

dbHelper = new DBHelper(MainActivity.this);

还要在这里添加,确保字符串比较以正确的方式完成..

How do I compare strings in Java?

以上链接可以帮助您。