0

私はアンドロイドが初めてで、ログインアクティビティで構成されるアプリケーションを設計しています。私は多くのことを試みていますが、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;
        }

    }
}

これは 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)
4

1 に答える 1

3

= 演算子を文字列比較で使用しているため、カーソルは null のままだと思います。

以下を試してください:

c = db.rawQuery("select * from login where user LIKE " + "\""+ s1.trim() + "\""+" and password LIKE "+ "\""+ p1.trim() + "\"", null);
                                                ^^^^                                           ^^^^
                                                ^^^^                                           ^^^^

また * は SQLite に有効ですか?

于 2012-08-04T09:49:23.580 に答える