我正在Android中创建一个登录应用程序,我已经完成了将数据库推送到模拟器中。当我使用我创建的应用程序添加数据库时,模拟器停止工作并停止响应。我该怎么办?
package com.android.sms.privacy;
import java.util.Arrays;
import java.util.Locale;
import com.android.sms.privacy.DatabaseHelper;
import android.app.Activity;
import android.widget.EditText;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;
public class SMS extends Activity {
//
public static final String DEBUG_TAG = "FullDatabase Log";
public static final int DB_version = 2;
//public static final String DB_name = "User.db";
public static final String TABLE_NAME = "login";
//public static final String TABLE_NAME = "tbl_temp";
//public static final String DB_name = "User_temp";
public static final String DBNAME = "login";
public DatabaseHelper database_helper;
public SQLiteDatabase database;
public SQLiteDatabase database_rd;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createaccount);
Toast.makeText(this,"Before Bundled",
Toast.LENGTH_SHORT).show();
createDataBase();
//LogIn login = new LogIn();
Button submit = (Button)findViewById(R.id.Button02);
Toast.makeText(this,"after Bundled",
Toast.LENGTH_SHORT).show();
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Welcome createaccount = new Welcome();
Toast.makeText(createaccount.getApplicationContext(), "In Helper executing onCreate1()",
Toast.LENGTH_SHORT).show();
String[] success = insertRecords();
if(success[0].equalsIgnoreCase("1"))
{
setContentView(R.layout.menu);
}
}
});
//database.close();
}
//
protected 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.username);
final EditText password = (EditText)findViewById(R.id.password);
Long newUserID = 0l;
String user = username.getText().toString();
String userpass = password.getText().toString();
ContentValues values = new ContentValues();
values.put("username",user);
values.put("password",userpass);
//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] = user;
info[2] = userpass;
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;
}
private void LogCursorInfo(Cursor c) {
// TODO Auto-generated method stub
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",
c.moveToNext();
}
Log.i(DEBUG_TAG, "*** Cursor End ***");
}
void createDataBase()
{
if(Arrays.binarySearch(databaseList(), DBNAME)>=0)
{
deleteDatabase(DBNAME);
}
database_helper = new
DatabaseHelper(this.getApplicationContext());
//database_helper.onCreate(database);
database = database_helper.getWritableDatabase();
database.setLocale(Locale.getDefault()); // Set the local
database.setLockingEnabled(true); // SQLiteDatabase is made thread-safe by using locks around critical sections.
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 local
database.setLockingEnabled(true); // SQLiteDatabase is made
thread-safe by using locks around critical sections.
database.setVersion(1);
database.execSQL(CREATE_USER_TABLE);
*/
}
}
此时应用程序停止响应:
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Welcome createaccount = new Welcome();
Toast.makeText(createaccount.getApplicationContext(), "In Helper executing onCreate1()",
Toast.LENGTH_SHORT).show();
String[] success = insertRecords();
if(success[0].equalsIgnoreCase("1"))
{
setContentView(R.layout.menu);
}
}
});
答案 0 :(得分:1)
我猜这里提出的问题
Welcome createaccount = new Welcome();
您已扩展Application类以获取应用程序上下文。但您可能无法在AndroidManifest.xml
文件中注册。因此,请在清单文件中为android:name="Welcome"
标记设置<application>
。