如何在数据库中插入edittext数据

时间:2015-09-03 07:42:21

标签: android sqlite

我想将数据存储在新用户的数据库中。我从这个网站上阅读了更多的答案。但我不明白如何解决这个错误,我的应用程序不幸被关闭了。

09-03 03:24:44.316: E/AndroidRuntime(1032): FATAL EXCEPTION: main
09-03 03:24:44.316: E/AndroidRuntime(1032): Process:                                            

com.jogatiservices.numbersinfo, PID: 1032
09-03 03:24:44.316: E/AndroidRuntime(1032): java.lang.NullPointerException
09-03 03:24:44.316: E/AndroidRuntime(1032):     at    
 com.jogatiservices.numbersinfo.RegisterActivity$2.onClick(RegisterActivity.java:64)
  09-03 03:24:44.316: E/AndroidRuntime(1032):   at                           
 android.view.View.performClick(View.java:4438)
 09-03 03:24:44.316: E/AndroidRuntime(1032):    at   
 android.view.View$PerformClick.run(View.java:18422)
 09-03 03:24:44.316: E/AndroidRuntime(1032):    at    
 android.os.Handler.handleCallback(Handler.java:733)
 09-03 03:24:44.316: E/AndroidRuntime(1032):    at 
android.os.Handler.dispatchMessage(Handler.java:95)
09-03 03:24:44.316: E/AndroidRuntime(1032):     at  
android.os.Looper.loop(Looper.java:136)
09-03 03:24:44.316: E/AndroidRuntime(1032):     at    
android.app.ActivityThread.main(ActivityThread.java:5017)
09-03 03:24:44.316: E/AndroidRuntime(1032):     at       
 java.lang.reflect.Method.invokeNative(Native Method)
09-03 03:24:44.316: E/AndroidRuntime(1032):     at      
java.lang.reflect.Method.invoke(Method.java:515)
09-03 03:24:44.316: E/AndroidRuntime(1032):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run         
(ZygoteInit.java:779)
09-03 03:24:44.316: E/AndroidRuntime(1032):     at    
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-03 03:24:44.316: E/AndroidRuntime(1032):     at    
dalvik.system.NativeStart.main(Native Method)

我的代码是

适配器类

public void insertEntry(String fullname, String email, String password)
            {
               ContentValues newValues = new ContentValues();
                // Assign values for each row.
                newValues.put("USERNAME", fullname);
                newValues.put("EMAILID", email);
                newValues.put("PASSWORD",password);

                // Insert the row into your table
                db.insert("USERSINFO", null, newValues);
                //Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
            }

注册课程

Button btnregister=(Button)findViewById(R.id.btnRegister);
    btnregister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub


    // getting database for reading/writing purpose
        db=dataBaseHelper.getWritableDatabase();

        String fullname=editTextUserName.getText().toString();
        String password=editTextPassword.getText().toString();
        String email=editTextEmail.getText().toString();
        String confirmPassword=editTextConfirmPassword.getText().toString();

        // check if any of the fields are empty or not
        if(fullname.equals("")||email.equals("")||password.equals("")||confirmPassword.equals(""))
        {
                Toast.makeText(getApplicationContext(), "Fill The Empty Field", Toast.LENGTH_LONG).show();
                return;
        }
        else if(!password.equals(confirmPassword))
        {

            Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
            return;

        }

        else
        {

            loginDataBaseAdapter.insertEntry(fullname, email, password);
            Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();


        }
    }
});

loginDataBaseAdapter.close();       
}

2 个答案:

答案 0 :(得分:0)

你得到一个NullPointerException。这意味着您正在尝试在null对象上调用方法。该行是文件中失败的行

(om.jogatiservices.numbersinfo.RegisterActivity$2.onClick(RegisterActivity.java:64) -> line 64 in RegisterActivity), but I don't know exactly which line that is.

但是我看到了一些问题。

String fullname=editTextUserName.getText().toString();

如果getText()为空,即如果该字段为空,会发生什么?

fullname.equals("")

这也是不好的做法(但我认为这不会失败)。如果你做"".equals(fullname)你是安全的,这将永远不会失败(因为“”永远不会为空)。如果fullname以某种方式结束为null,那么你的方法将会失败。

答案 1 :(得分:0)

检查您的字段

if(fullname!=null) || .....)

像这样使用它。

public void insertEntry(String fullname, String email, String password)
        {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("USERNAME", fullname);
            newValues.put("EMAILID", email);
            newValues.put("PASSWORD",password);

            // Insert the row into your table
            db.insert("USERSINFO", null, newValues);
            //Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        db.close();
    }

并且不要在外面打开数据库(bcoz需要在行动后关闭它)功能。

也可以使用AsyncTask进行数据库或网络操作,这是更好的方法。