unexpectedly stopped error storing text boxes in to database table dynamically in android
I am new to android application development.I have create registration form with user name,password, email. this values should be stored in sqlite database table. retrieve the data from the table.
in login.java i have create the table and insert the values of registration form data in to the table dynamically. In databsehelper.java extends the databaseopenHelper class overwrite the oncreate and upgrade methods.this class i used this class in login.java.
In display.java i am going to display the data in on success register details.
there are no errors but it gives the error on the emulator unexpectedly stopped. I am putting code below.
login.java
package rt.prj;
import java.util.Arrays;
import java.util.Locale;
import rt.prj.DatabaseHelper;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class realestatenew extends Activity {
/** Called when the activity is first created. */
public static final String DEBUG_TAG = "FullDatabase Log";
public static final int DB_version = 1;
//public static final String DB_name = "User.db";
public static final String TABLE_NAME = "tbl_users";
//public static final String TABLE_NAME = "tbl_temp";
//public static final String DB_name = "User_temp";
public static final String DB_name = "User";
public static final String CREATE_USER_TABLE = "CREATE TABLE"+TABLE_NAME+"(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT,lastname TEXT, username TEXT, password TEXT, email TEXT);";
public static final String DROP_USER_TABLE = "DROP TABLE tbl_users;";
public DatabaseHelper database_helper;
public SQLiteDatabase database;
public SQLiteDatabase database_rd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Button register=(Button)findViewById(R.id.Button01);
//private EditText text1;
//Registration database creation
createDataBase();
register.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
realestatenew login = new realestatenew();
Toast.makeText(login.getApplicationContext(),"In Helper executin onCreate()", Toast.LENGTH_SHORT).show();
String[] success = insertRecords();
if(success[0].equalsIgnoreCase("1"))
{
Intent mIntent = new Intent(realestatenew.this,DisplayUser.class);
mIntent.putExtra("username",success[1]);
mIntent.putExtra("password",success[2]);
mIntent.putExtra("email",success[3]);
startActivity(mIntent);
}
}
});
}
//database create function
void createDataBase()
{
if(Arrays.binarySearch(databaseList(), DB_name)>=0)
{
deleteDatabase(DB_name);
}
database_helper = new
DatabaseHelper(this.getApplicationContext(),DB_name,null,DB_version);
//database_helper.onCreate(database);
database = database_helper.getWritableDatabase();
database.setLocale(Locale.getDefault());
// Set the locale
database.setLockingEnabled(true);
database_rd = database_helper.getReadableDatabase();
Toast.makeText(this, "created database",
Toast.LENGTH_SHORT).show();
/*
database =
openOrCreateDatabase(DB_name,SQLiteDatabase.CREATE_IF_NECESSARY,
null);
database.setLocale(Locale.getDefault()); // Set the locale
database.setLockingEnabled(true); // SQLiteDatabase is made
thread-safe by using locks around critical sections.
database.setVersion(1);
database.execSQL(CREATE_USER_TABLE);
*/
}
// data insertion
String[] insertRecords()
{
Toast.makeText(this,"In insertrecords",
Toast.LENGTH_SHORT).show();
//database.openDatabase(DB_name,database.create(factory),ACCESSIBILITY_SERVICE);
final EditText username = (EditText) findViewById(R.id.EditText01);
final EditText password= (EditText) findViewById(R.id.EditText02);
final EditText email= (EditText) findViewById(R.id.EditText04);
Long newUserID = 0l;
String uname = username.getText().toString();
String pwd = password.getText().toString();
String emailid = email.getText().toString();
ContentValues values = new ContentValues();
values.put("username",uname);
values.put("password",pwd);
values.put("email",emailid);
//StringBuilder info = new StringBuilder();
String[] info = new String[10];
database.beginTransaction();
try
{
Toast.makeText(this,"Before calling inserting values",
Toast.LENGTH_SHORT).show();
newUserID = database.insertOrThrow(TABLE_NAME, null, values);
Toast.makeText(this,"After calling inserting values",
Toast.LENGTH_SHORT).show();
info[0] = newUserID.toString();
info[1] = uname;
info[2] = pwd;
info[3] = emailid;
System.out.println(newUserID);
Toast.makeText(this, newUserID.toString(),
Toast.LENGTH_SHORT).show();
Cursor c = database_rd.query(TABLE_NAME, null, null, null, null,null,null);
LogCursorInfo(c);
c.close();
}
catch(Exception e)
{
//Transaction failed;
e.printStackTrace();
}
finally
{
database.endTransaction();
database.close();
}
return info;
}
public void LogCursorInfo(Cursor c)
{
Log.i(DEBUG_TAG, "*** Cursor Begin *** " + " Results:" +
c.getCount() + " Columns: " + c.getColumnCount());
// Print column names
String rowHeaders = "|| ";
for (int i = 0; i < c.getColumnCount(); i++) {
rowHeaders = rowHeaders.concat(c.getColumnName(i) +"||");
}
Log.i(DEBUG_TAG, "COLUMNS " + rowHeaders);
//Integer rows = c.getCount();
//Toast.makeText(this, rows.toString(),
// Toast.LENGTH_SHORT).show();
// Print records
c.moveToFirst();
while (c.isAfterLast() == false) {
String rowResults = "|| ";
for (int i = 0; i < c.getColumnCount(); i++) {
rowResults = rowResults.concat(c.getString(i) +
" || ");
}
Log.i(DEBUG_TAG, "Row " + c.getPosition() + ": " +
rowResults);
Toast.makeText(this, rowResults.toString(),
Toast.LENGTH_SHORT).show();
//Toast.makeText(this, "In LogCursorInfo",
// Toast.LENGTH_SHORT).show();
c.moveToNext();
}
Log.i(DEBUG_TAG, "*** Cursor End ***");
}
}
databasehelper.java
package rt.prj;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.widget.Toast;
public class DatabaseHelper extends SQLiteOpenHelper
{
public static final int DB_version = 1;
public static final String DB_name = "realestate.db";
public SQLiteDatabase database;
public static final String TABLE_NAME = "tbl_users";
//public static final String TABLE_NAME = "tbl_temp";
public static final String CREATE_USER_TABLE = "CREATE TABLE"+TABLE_NAME+"(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT,lastname TEXT, username TEXT, password TEXT, email TEXT);";
realestatenew login = new realestatenew();
public DatabaseHelper(Context context, String name, CursorFactory
factory,int version)
{
super(context, DB_name, factory,DB_version);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db)
{
Toast.makeText(login.getApplicationContext(),"In Helper executingonCreate()", Toast.LENGTH_SHORT).show();
// Create the table
db.execSQL("DROP TABLE IF EXISTS tbl_users");
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Housekeeping here.
// Implement how "move" your application data during an upgrade of schema versions
// There is no ALTER TABLE command in SQLite, so this generally involves
// CREATING a new table, moving data if possible, or deleting the old data and starting fresh
// Your call.
//db.execSQL("DROP TABLE IF EXISTS tbl_users");
//onCreate(database);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
}
display.java
package rt.prj;
//import java.util.Locale;
import android.app.Activity;
//import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import rt.prj.*;
public class DisplayUser extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.userinfo);
final String DB_name = "realestate.db";
final String TABLE_NAME = "tbl_users";
final int DB_version = 1;
//final DatabaseHelper database_helper;
//final SQLiteDatabase database_wr;
/*
database =
openOrCreateDatabase(DB_name,SQLiteDatabase.CREATE_IF_NECESSARY,
null);
database.setLocale(Locale.getDefault()); // Set the locale
database.setLockingEnabled(true); // SQLiteDatabase is made
thread-safe by using locks around critical sections.
database.setVersion(1);
if(database.isOpen())
{
Toast.makeText(this, "Yes database is open",
Toast.LENGTH_SHORT).show();
}
if(database.isDbLockedByCurrentThread())
{
Toast.makeText(this, "Yes database is locked",
Toast.LENGTH_SHORT).show();
}
Cursor c = database.query(TABLE_NAME, null, null, null, null,
null,null);
LogIn login = new LogIn();
login.LogCursorInfo(c);
c.close();
*/
//LogIn login = new LogIn();
//database_wr = login.database_helper.getWritableDatabase();
String uname= getIntent().getStringExtra("username");
String pwd= getIntent().getStringExtra("password");
String email= getIntent().getStringExtra("email");
Toast.makeText(this, uname, Toast.LENGTH_SHORT).show();
View infoPanel = _createInfoPanel(uname,pwd,email);
setContentView(infoPanel);
}
public View _createInfoPanel(String uname,String pwd,String
email)
{
LinearLayout panel = new LinearLayout(this);
panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
panel.setOrientation(LinearLayout.VERTICAL);
// user id
TextView Welcome = new TextView(this);
Welcome.setText("Welcome "+uname);
Welcome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
Welcome.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView UserName = new TextView(this);
UserName.setText("Username:"+uname);
UserName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
UserName.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView pass = new TextView(this);
pass.setText("password:"+pwd);
pass.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
pass.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
开发者_如何学编程 TextView Email = new TextView(this);
Email.setText("E-mailID:"+email);
Email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
Email.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
panel.addView(Welcome);
panel.addView(UserName);
panel.addView(pass);
panel.addView(Email);
return panel;
}
}
Not sure why this is happening and I can't really read the code because it isn't formatted correctly, but if you want to store something as trivial as some text strings you really should use SharedPreferences. Using SharedPreferences you could store and retrieve the data in about 5 lines of code.
final SharedPreferences prefs = context.getSharedPreferences();
Editor editor = prefs.edit();
editor.putString("email", email);
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
email = prefs.getString("email");
精彩评论