简体   繁体   English

Android数据库应用程序出了问题

[英]Android Database Application going wrong

I have created a simple Android application which asks for the users information such as name, blood type, contact number, etc., saves it in a database, and displays it in another screen. 我创建了一个简单的Android应用程序,该应用程序要求用户提供姓名,血型,联系电话等信息,并将其保存在数据库中,并在另一个屏幕中显示。 This is all suppose to happen when the save button is clicked which invokes the saveMe() method. 所有这一切都发生在单击保存按钮并调用saveMe()方法时。 However, when I click the save button, the app does not crash and close instead it gets stuck and then in the logcat I see something like: 但是,当我单击“保存”按钮时,该应用程序不会崩溃并关闭,而是卡住了,然后在logcat看到了类似以下内容:

08-08 22:41:23.734: D/dalvikvm(365): GC_FOR_MALLOC freed 321K, 50% free 2984K/5959K, external 716K/1038K, paused 18ms
08-08 22:41:23.754: D/dalvikvm(365): GC_FOR_MALLOC freed 322K, 50% free 2984K/5959K, external 716K/1038K, paused 18ms
08-08 22:41:23.784: D/dalvikvm(365): GC_FOR_MALLOC freed 323K, 50% free 2984K/5959K, external 716K/1038K, paused 21ms
08-08 22:41:23.804: D/dalvikvm(365): GC_FOR_MALLOC freed 324K, 50% free 2984K/5959K, external 716K/1038K, paused 19ms
08-08 22:41:23.824: D/dalvikvm(365): GC_FOR_MALLOC freed 324K, 50% free 2984K/5959K, external 716K/1038K, paused 20ms
08-08 22:41:23.844: D/dalvikvm(365): GC_FOR_MALLOC freed 325K, 50% free 2984K/5959K, external 716K/1038K, paused 20ms
08-08 22:41:23.874: D/dalvikvm(365): GC_FOR_MALLOC freed 326K, 50% free 2984K/5959K, external 716K/1038K, paused 25ms
08-08 22:41:23.894: D/dalvikvm(365): GC_FOR_MALLOC freed 327K, 50% free 2984K/5959K, external 716K/1038K, paused 23ms
08-08 22:41:23.934: D/dalvikvm(365): GC_FOR_MALLOC freed 327K, 50% free 

These messages keep appearing in log cat. 这些消息一直出现在日志目录中。 The only way I can stop them is if I close the emulator. 阻止它们的唯一方法是关闭模拟器。 Is the app getting stuck in a infinite loop? 应用是否陷入无限循环? No data get saved or displayed. 没有数据被保存或显示。

Database Class 数据库类

package com.example.androidsimpledbapp1;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;


public class MyDBHandler extends SQLiteOpenHelper {

/*
 * Class for Working with DB 
 */

//Update each time DB structure changes e.g. adding new property
private static final int DATABASE_VERSION =1;
//DB Name
private static final String DATABASE_NAME = "deets.db";
//Table name
public static final String  TABLE_PRODUCTS = "products";
//DB Columns 
public static final String  COLUMN_ID = "_Id";
public static final String  COLUMN_PERSONNAME  = "firstName";
public static final String  COLUMN_PERSONBLOOD  = "bloodType";
public static final String  COLUMN_PERSONCONTACT  = "contactName";
public static final String  COLUMN_PERSONNUMBER  = "phoneNumber";
public static final String  COLUMN_PERSONRELATION = "relationship";

//Constructor
/*
 * Passing information to super class in SQL
 * Context is background information 
 * name of db 
 * Database version
 */
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}


/*
 * What to do first time when you create DB
 * Creates the table the very first time
 * (non-Javadoc)
 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
 * Remember to use Commas as shown below
 */
@Override
public void onCreate(SQLiteDatabase db){
    String query = "CREATE TABLE "+ TABLE_PRODUCTS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
            COLUMN_PERSONNAME + " TEXT, "+
            COLUMN_PERSONBLOOD + " TEXT, "+
            COLUMN_PERSONCONTACT + " TEXT, "+
            COLUMN_PERSONNUMBER + " TEXT, " +
            COLUMN_PERSONRELATION + " TEXT " +
            ");";
    //Execute the query
    db.execSQL(query);
}

/*
 * If ever upgrading DB call this method
 * (non-Javadoc)
 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
 */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    //Delete the current table
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_PRODUCTS);
    //create new table 
    onCreate(db);
}


//Add new row to the database
public void addProduct(Details details){
    //Built in class - set values for different columns 
    //Makes inserting rows quick and easy
    ContentValues values = new ContentValues();
    values.put(COLUMN_PERSONNAME, details.get_firstName());
    values.put(COLUMN_PERSONBLOOD, details.get_bloodType());
    values.put(COLUMN_PERSONCONTACT, details.get_contactName());
    values.put(COLUMN_PERSONNUMBER, details.get_phoneNumber());
    values.put(COLUMN_PERSONRELATION, details.get_relationship());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_PRODUCTS, null, values);
    db.close();
}

/*
public void deleteProducts(){
    SQLiteDatabase = getWritableDatabase();
    db.execSQL("DROP TABLE");

    How to delete the database...
}
*/


//Take DB and Convert to String 
public String databaseToString(){
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    //Every Column and row
    String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

    //Cursor points to a location in your results
    //First row point here, second row point here

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();

    while(!c.isAfterLast()){
        //Extracts first name and adds to string
        if(c.getString(c.getColumnIndex("firstName"))!=null){
            dbString += c.getString(c.getColumnIndex("firstName"));
            /*
             * Displaying all other columns 
             */
        }
    }
    db.close();
    return dbString;
}


}

Details Class 详细类别

package com.example.androidsimpledbapp1;

public class Details {

//primary key
private int _id;
//Properties 
private String _firstName;
private String _bloodType;
private String _contactName;
private String _phoneNumber;
private String _relationship;

//Dont Have to Enter Everything each time
public Details(){

}

public Details(String firstName){
    this.set_firstName(firstName);
}

//Passing in details 
//Setting values from the user 
public Details(String firstName, String bloodType,
        String contactName, String phoneNumber,
        String relationship){
    this.set_firstName(firstName);
    this.set_bloodType(bloodType);
    this.set_contactName(contactName);
    this.set_phoneNumber(phoneNumber);
    this.set_relationship(relationship);

}

//Retrieve the data 
public int get_id() {
    return _id;
}

//Setter allows to give property
public void set_id(int _id) {
    this._id = _id;
}

public String get_firstName() {
    return _firstName;
}

public void set_firstName(String _firstName) {
    this._firstName = _firstName;
}

public String get_bloodType() {
    return _bloodType;
}

public void set_bloodType(String _bloodType) {
    this._bloodType = _bloodType;
}

public String get_contactName() {
    return _contactName;
}

public void set_contactName(String _contactName) {
    this._contactName = _contactName;
}

public String get_phoneNumber() {
    return _phoneNumber;
}

public void set_phoneNumber(String _phoneNumber) {
    this._phoneNumber = _phoneNumber;
}

public String get_relationship() {
    return _relationship;
}

public void set_relationship(String _relationship) {
    this._relationship = _relationship;
}

 }

Edit Screen - Where the save button is clicked 编辑屏幕-单击保存按钮的位置

public class EditScreen extends Activity {

EditText firstNameInput;

EditText bloodTypeInput;
EditText contacNameInput;
EditText phoneNumberInput;
EditText relationshipInput;

TextView displayName;
MyDBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_screen);
    firstNameInput = (EditText) findViewById(R.id.inputname);
    bloodTypeInput = (EditText) findViewById(R.id.inputblood);
    contacNameInput = (EditText) findViewById(R.id.inputcontact);
    phoneNumberInput = (EditText) findViewById(R.id.inputnum);
    relationshipInput = (EditText) findViewById(R.id.inputraltion);
    displayName = (TextView) findViewById(R.id.dbname);
    dbHandler = new MyDBHandler(this, null, null, 1);

}

/*
 * Causing error fix the error
 */
public void saveMe(View v){
    Details detail = new Details(firstNameInput.getText().toString(),
            bloodTypeInput.getText().toString(),
            contacNameInput.getText().toString(),
            phoneNumberInput.getText().toString(),
            relationshipInput.getText().toString()
            );
    dbHandler.addProduct(detail);
    printDatabase();
}

private void printDatabase() {
    //Taking the string
    String dbString = dbHandler.databaseToString();
    //Display in the textview
    displayName.setText(dbString);
}

 }

Main Activity 主要活动

package com.example.androidsimpledbapp1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

//Changing Activity
public void editBtnPressed(View v){
    Intent intent = new Intent(MainActivity.this, EditScreen.class);
    startActivity(intent);
}

  }

Logcat - Error Logcat-错误

08-08 22:41:00.034: D/AndroidRuntime(348): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-08 22:41:00.054: D/AndroidRuntime(348): CheckJNI is ON
08-08 22:41:00.273: I/ActivityManager(60): Start proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver: pid=350 uid=10028 gids={}
08-08 22:41:00.583: I/ActivityThread(350): Pub com.svox.pico.providers.SettingsProvider: com.svox.pico.providers.SettingsProvider
08-08 22:41:00.663: D/GTalkService(185): handlePackageInstalled: re-initialize providers
08-08 22:41:00.663: D/GTalkService(185): [RawStanzaProvidersMgr] ##### searchProvidersFromIntent
08-08 22:41:00.663: D/GTalkService(185): [RawStanzaProvidersMgr] no intent receivers found
08-08 22:41:00.713: D/dalvikvm(335): GC_CONCURRENT freed 211K, 43% free 3575K/6215K, external 716K/1038K, paused 3ms+4ms
08-08 22:41:00.874: D/AndroidRuntime(348): Calling main entry com.android.commands.am.Am
08-08 22:41:00.904: I/ActivityManager(60): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.androidsimpledbapp1/.MainActivity } from pid 348
08-08 22:41:00.974: D/AndroidRuntime(348): Shutting down VM
08-08 22:41:00.984: I/ActivityManager(60): Start proc com.example.androidsimpledbapp1 for activity com.example.androidsimpledbapp1/.MainActivity: pid=365 uid=10045 gids={}
08-08 22:41:00.994: D/dalvikvm(348): GC_CONCURRENT freed 101K, 69% free 318K/1024K, external 0K/0K, paused 1ms+1ms
08-08 22:41:01.004: D/jdwp(348): adbd disconnected
08-08 22:41:01.154: D/dalvikvm(32): GC_EXPLICIT freed 11K, 50% free 2719K/5379K, external 716K/1038K, paused 168ms
08-08 22:41:01.214: D/dalvikvm(32): GC_EXPLICIT freed <1K, 50% free 2719K/5379K, external 716K/1038K, paused 62ms

你错过c.moveToNext()在你的databaseToString() while循环和循环永远不会终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM