我是 android 新手,正在设计一个包含登录活动的应用程序。我尝试了很多,但是在使用 sqlite 数据库检查用户名和密码字段时得到了 java.lang.nullPointerException。这是代码。
public int validateLogin(String s1, String p1) throws SQLException {
// TODO Auto-generated method stub
try
{
int i = 0;
Cursor c = null;
c = db.rawQuery("select * from login where user =" + "\""+ s1.trim() + "\""+" and password="+ "\""+ p1.trim() + "\"", null);
c.moveToFirst();
i = c.getCount();
c.close();
return i;
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
public class UserPass extends Activity implements OnClickListener {
EditText name;
EditText password;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
EditText name=(EditText)findViewById(R.id.edit1);
EditText password=(EditText)findViewById(R.id.edit2);
Button logit=(Button)findViewById(R.id.loginb);
Button registerit=(Button)findViewById(R.id.registerb);
TextView textt=(TextView)findViewById(R.id.txt);
logit.setOnClickListener(this);
registerit.setOnClickListener(this);
// final String s2="user";
// final String p2="pass";
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.loginb:
//boolean valid=false;
//try{
String s1=name.getText().toString();
String p1=password.getText().toString();
DBAdapter checkdb=new DBAdapter(UserPass.this);
checkdb.open();
// checkdb.check(s1, p1);
checkdb.validateLogin(s1, p1);
if(true){
Toast.makeText(UserPass.this, "logged in",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(UserPass.this, "",Toast.LENGTH_LONG).show();
}
checkdb.close();
/**}catch(Exception e){
valid=false;
String error=e.toString();
Toast.makeText(UserPass.this, error,Toast.LENGTH_LONG).show();
}finally{
if(){
Toast.makeText(UserPass.this, "logged in",Toast.LENGTH_LONG).show();
}
}
break;*/
case R.id.registerb:
Intent ireg=new Intent(UserPass.this, register.class);
startActivity(ireg);
break;
}
}
}
这是 m 在 log-cat 中出现的错误
08-04 18:30:48.524: W/KeyCharacterMap(481): No keyboard for id 0
08-04 18:30:48.524: W/KeyCharacterMap(481): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-04 18:30:57.694: W/dalvikvm(481): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-04 18:30:57.714: E/AndroidRuntime(481): FATAL EXCEPTION: main
08-04 18:30:57.714: E/AndroidRuntime(481): java.lang.NullPointerException
08-04 18:30:57.714: E/AndroidRuntime(481): at com.inovawe.userpass.UserPass.onClick(UserPass.java:49)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.View.performClick(View.java:2408)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.View.onKeyUp(View.java:4121)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.widget.TextView.onKeyUp(TextView.java:4431)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.KeyEvent.dispatch(KeyEvent.java:1061)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.View.dispatchKeyEvent(View.java:3740)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-04 18:30:57.714: E/AndroidRuntime(481): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
08-04 18:30:57.714: E/AndroidRuntime(481): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
08-04 18:30:57.714: E/AndroidRuntime(481): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.os.Looper.loop(Looper.java:123)
08-04 18:30:57.714: E/AndroidRuntime(481): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-04 18:30:57.714: E/AndroidRuntime(481): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 18:30:57.714: E/AndroidRuntime(481): at java.lang.reflect.Method.invoke(Method.java:521)
08-04 18:30:57.714: E/AndroidRuntime(481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-04 18:30:57.714: E/AndroidRuntime(481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-04 18:30:57.714: E/AndroidRuntime(481): at dalvik.system.NativeStart.main(Native Method)