每当我尝试在手机上调试应用程序时,就会发生此错误...
我不知道是什么问题?
错误是:MY LOGCAT WINDOW
我的DatabaseHelper类
package com.example.adityasinh.healthassitance;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION =1;
private static final String DATABASE_NAME ="contact.db";
private static final String TABLE_NAME ="contacts";
SQLiteDatabase db;
private static final String CREATE_QUERY =
"CREATE TABLE "
+ Contact.NewUserInfo.TABLE_NAME+" ("
+ Contact.NewUserInfo.First_Name+" TEXT NOT NULL,"
+ Contact.NewUserInfo.Last_Name+" TEXT NOT NULL,"
+ Contact.NewUserInfo.Gender+" TEXT NOT NULL,"
+ Contact.NewUserInfo.Age+" TEXT NOT NULL,"
+ Contact.NewUserInfo.Mob_No+" TEXT NOT NULL,"
+ Contact.NewUserInfo.Pass1+" TEXT NOT NULL,"
+ Contact.NewUserInfo.Pass2+" TEXT NOT NULL,"
+ Contact.NewUserInfo.Emailid+" TEXT NOT NULL );";
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","DB created / opened........");
}
public void insertContact(String first_name, String last_name, String gender, String age, String mob, String emailid, String pass1,String pass2,SQLiteDatabase db)
{
db=this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues contentValues = new ContentValues();
contentValues.put(Contact.NewUserInfo.First_Name,first_name);
contentValues.put(Contact.NewUserInfo.Last_Name,last_name);
contentValues.put(Contact.NewUserInfo.Gender,gender);
contentValues.put(Contact.NewUserInfo.Age,age);
contentValues.put(Contact.NewUserInfo.Mob_No,mob);
contentValues.put(Contact.NewUserInfo.Emailid,emailid);
contentValues.put(Contact.NewUserInfo.Pass1,pass1);
contentValues.put(Contact.NewUserInfo.Pass2,pass2);
// db.insert(Contact.NewUserInfo.TABLE_NAME, null, contentValues);
Log.e("DATABASE OPERATIONS", "One Row Inserted...");
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String Emailid){
db = this.getReadableDatabase();
String query = "Select email and password from"+TABLE_NAME;
Cursor cursor = db.rawQuery (query , null );
cursor.getInt(0);
String a,b;
b="Not Found";
if(cursor.moveToFirst())
{
do{
a=cursor.getString(0);
if(a.equals(Emailid))
{
b=cursor.getString(1);
break;
}
}while(cursor.moveToNext());
}
return b;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS","Table created......");
this.db = db;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXITS"+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
}
我的联系方式
package com.example.adityasinh.healthassitance;
public class Contact {
public static abstract class NewUserInfo
{
public static final String First_Name = "first_name";
public static final String Last_Name = "last_name";
public static final String Gender = "gender";
public static final String Age = "age";
public static final String Mob_No ="mob_no";
public static final String Emailid ="emailid";
public static final String Pass1 ="pass1";
public static final String Pass2 ="pass2";
public static final String TABLE_NAME = "contacts";
}
}
我的SignupActivity类
package com.example.adityasinh.healthassitance;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SignupActivity extends Activity{
Context context= this;
SQLiteDatabase sqLiteDatabase;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
}
public void onSignUpClick(View v)
{
if(v.getId()==R.id.Bsignupbutton)
{
EditText first_name= (EditText) findViewById(R.id.firstname);
EditText last_name=(EditText) findViewById(R.id.lastname);
EditText gender=(EditText) findViewById(R.id.gender);
EditText age=(EditText) findViewById(R.id.age);
EditText mobileno=(EditText) findViewById(R.id.mobileno);
EditText emailid=(EditText) findViewById(R.id.emailid1);
EditText pass1=(EditText) findViewById(R.id.pass1);
EditText pass2=(EditText) findViewById(R.id.pass2);
String firstnamestr= first_name.getText().toString();
String lastnamestr= last_name.getText().toString();
String genderstr= gender.getText().toString();
String agestr= age.getText().toString();
String mobilenostr= mobileno.getText().toString();
String emailidstr= emailid.getText().toString();
String pass1str= pass1.getText().toString();
String pass2str= pass2.getText().toString();
if(!pass1str.equals(pass2str))
{
Toast pass = Toast.makeText(SignupActivity.this,"Password Don't Match",Toast.LENGTH_SHORT);
pass.show();
}
else {
////insert details in DB
databaseHelper=new DatabaseHelper(context);
sqLiteDatabase= databaseHelper.getWritableDatabase();
databaseHelper.insertContact(firstnamestr , lastnamestr , genderstr , agestr , mobilenostr , emailidstr , pass1str , pass2str , sqLiteDatabase);
ContentValues contentValues = new ContentValues();
contentValues.put(Contact.NewUserInfo.First_Name,firstnamestr);
contentValues.put(Contact.NewUserInfo.Last_Name,lastnamestr);
contentValues.put(Contact.NewUserInfo.Gender,genderstr);
contentValues.put(Contact.NewUserInfo.Age,agestr);
contentValues.put(Contact.NewUserInfo.Mob_No,mobilenostr);
contentValues.put(Contact.NewUserInfo.Emailid,emailidstr);
//contentValues.put(Contact.NewUserInfo,Pass1,pass1str);
//contentValues.put(Contact.NewUserInfo,Pass2,pass2str);
Toast.makeText(getBaseContext(),"Data Saved",Toast.LENGTH_LONG).show();
databaseHelper.close();
}
}
}
}
还有我的LoginActivity类
package com.example.adityasinh.healthassitance;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
DatabaseHelper helper=new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void onButtonClick(View v)
{
if(v.getId()==R.id.Blogin)
{
EditText a = (EditText) findViewById(R.id.emailid1);
String str= a.getText().toString();
EditText b = (EditText) findViewById(R.id.password);
String pass1= b.getText().toString();
String password= helper.searchPass(str);
if(pass1.equals(password))
{
Intent i = new Intent(LoginActivity.this,DisplayActivity.class);
startActivity(i);
}
else {
Toast temp = Toast.makeText(LoginActivity.this,"UserName And Password Don't Match",Toast.LENGTH_SHORT);
temp.show();
}
}
if(v.getId()==R.id.Bsignup)
{
Intent i = new Intent(LoginActivity.this,SignupActivity.class);
startActivity(i);
}
}
}
最佳答案
两件事情:
您两次打开一个可写的Databse:
在SignupActivity的onSignUpClick函数中
在您的DatabaseHelper(insertContact)中
无需在onSignupClick中打开数据库!您不能同时使用2个可写数据库。
您的insertContact函数中有一个错误:
public void insertContact(String first_name, String last_name, String gender, String age, String mob, String emailid, String pass1,String pass2,SQLiteDatabase db)
{
db=this.getWritableDatabase();
ContentValues values = new ContentValues(); // <- why?
ContentValues contentValues = new ContentValues();
contentValues.put(Contact.NewUserInfo.First_Name,first_name);
contentValues.put(Contact.NewUserInfo.Last_Name,last_name);
contentValues.put(Contact.NewUserInfo.Gender,gender);
contentValues.put(Contact.NewUserInfo.Age,age);
contentValues.put(Contact.NewUserInfo.Mob_No,mob);
contentValues.put(Contact.NewUserInfo.Emailid,emailid);
contentValues.put(Contact.NewUserInfo.Pass1,pass1);
contentValues.put(Contact.NewUserInfo.Pass2,pass2);
// db.insert(Contact.NewUserInfo.TABLE_NAME, null, contentValues);
Log.e("DATABASE OPERATIONS", "One Row Inserted...");
db.insert(TABLE_NAME, null, values); // <- this is the empty ContentValues. Should be contentValues instead of values
db.close();
}