简体   繁体   English

getReadableDatabase崩溃,未调用onCreate

[英]getReadableDatabase crashing, onCreate not being called

How is it in this code that the DatabaseHandler does not execute onCreate() ? 在此代码中,DatabaseHandler不执行onCreate()如何? This original list app runs great and I am in the beginning stages of trying to inject a database into the app so the list displays records instead of coded strings. 这个原始列表应用程序运行良好,我正处于尝试向应用程序中注入数据库的初期阶段,因此列表显示记录而不是编码字符串。

The Android Intellisense shows the availability of the methods but the application crashes when run. Android Intellisense显示了方法的可用性,但应用程序在运行时崩溃。 My suspicion is the onCreate() never gets called because the Log.d messages never show up in the logcat. 我怀疑onCreate()永远不会被调用,因为Log.d消息永远不会出现在logcat中。 I have tried many combinations of the db instantiate or context passing to no avail. 我尝试了数据库实例化或上下文传递的许多组合都无济于事。

I am pulling the logcat from the phone with adb logcat > lr.txt as I can not run an emulator because the development machine is AMD and not an Intel processor. 我正在使用adb logcat> lr.txt从电话中提取logcat,因为我无法运行仿真器,因为开发计算机是AMD而不是Intel处理器。

Here is the db method class: 这是db方法类:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import android.widget.TextView;

public class DatabaseHandler extends SQLiteOpenHelper {

// for our logs
public static final String TAG = "DatabaseHandler.java";

public TextView tvstatus;

// database version
private static final int DATABASE_VERSION = 8;

// database name
protected static final String DATABASE_NAME = "LoadRunner";

// table details
public String tableName = "loadrunner";
public String fieldObjectId = "id";
public String fieldObjectName = "name";
public String fieldObjectDescription = "description";

// constructor
public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// creating table
@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "";
    this.tvstatus.setText("Creating table");

    sql += "CREATE TABLE " + tableName;
    sql += " ( ";
    sql += fieldObjectId + " INTEGER PRIMARY KEY AUTOINCREMENT, ";
    sql += fieldObjectName + " TEXT, ";
    sql += fieldObjectDescription + " TEXT ";
    sql += " ) ";

    Log.d("Loadrunner", "Create table");
    db.execSQL(sql);
    this.tvstatus.setText("Table created...");

    // create the index for our INSERT OR REPLACE INTO statement.
    // this acts as the WHERE name="name input" AND description="description input"
    // if that WHERE clause is true, I mean, it finds the same name and description in the database,
    // it will be REPLACEd. 
    // ELSE, what's in the database will remain and the input will be INSERTed (new record)
    String INDEX = "CREATE UNIQUE INDEX locations_index ON " 
                    + tableName + " (name, description)";

    db.execSQL(INDEX);
}

/*
 * When upgrading the database, it will drop the current table and recreate.
 */

Here is the Mainactivity method: TableMainLayout is the next activity called that displays the list. 这是Mainactivity方法:TableMainLayout是显示该列表的下一个活动。 I received the original code from codeofaninja.com and am adding the database to it. 我从codeofaninja.com收到了原始代码,并正在向其中添加数据库。 When I remark out the getreadabledatabase() the app runs fine with no database calls or records needed. 当我注释掉getizabledatabase()时,该应用程序可以正常运行,而无需数据库调用或记录。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* Loads next module */

   db = new DatabaseHandler(this);
   db.getReadableDatabase();
    //this.db.onCreate(SQLiteDatabase this.db);
    Log.d("Loadrunner ", "Loadrunner MainActivity DB var");
    setContentView(new TableMainLayout(this));
    Log.d("Loadrunner ", "Loadrunner MainActivity Content set");
    //this.db.insertFast(40);

Here is the logcat: 这是logcat:

D/Database( 5891): dbopen(): path = /data/data/com.example.tablefreezepane/databases/LoadRunner, flag = 6, file size = 3072 D /数据库(5891):dbopen():路径= /data/data/com.example.tablefreezepane/databases/LoadRunner,标志= 6,文件大小= 3072

D/Database( 5891): dbopen(): path = /data/data/com.example.tablefreezepane/databases/LoadRunner, mode: delete, disk free size: 64 M, handle: 0x336f70 D /数据库(5891):dbopen():路径= /data/data/com.example.tablefreezepane/databases/LoadRunner,模式:删除,可用磁盘大小:64 M,句柄:0x336f70

W/ContentService( 121): binderDied() at ObserverNode name com.google.android.gsf.gservices W / ContentService(121):ObserverNode名称为com.google.android.gsf.gservices的inderDied()

D/Database( 5891): dbclose(): path = /data/data/com.example.tablefreezepane/databases/LoadRunner, handle = 0x336f70 D /数据库(5891):dbclose():路径= /data/data/com.example.tablefreezepane/databases/LoadRunner,手柄= 0x336f70

D/AndroidRuntime( 5891): Shutting down VM D / AndroidRuntime(5891):关闭VM

And the app crash on the phone: 应用程序在手机上崩溃:

The Application LoadRunner(process com.example.tablefreezepane) has stopped unexpectedly. Application LoadRunner(进程com.example.tablefreezepane)已意外停止。 Please try again. 请再试一次。

Found issue!: this.tvstatus.setText("Table created..."); 发现问题!:this.tvstatus.setText(“表已创建...”); It was an activity problem all along. 一直以来都是活动问题。 I dont think I have that form on the screen for display. 我不认为我在屏幕上显示该表格。 I remmed it out and the app continued. 我重新整理了一下,应用程序继续运行。 So now I can pursue 2 issues. 所以现在我可以解决2个问题。 Clean up the display and start investigating the table created. 清理显示并开始调查创建的表。 Its on the phone. 它在电话上。 Thank you @Jemshit for the guidance. 谢谢@Jemshit的指导。 I'll take any more you care to offer. 我会再请您提供。 Oh, its a good day! 哦,今天真好! -

I don't see you get a reference of the textview. 我看不到您获得了textview的引用。 so tvstatus is pointing to nothing when you call.settext. 因此,当您调用.settext时,tvstatus没有指向任何内容。 this leads to nopointerexception.. use findviewbyid to get a reference first. 这导致nopointerexception ..使用findviewbyid首先获取引用。

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

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