繁体   English   中英

getReadableDatabase崩溃,未调用onCreate

[英]getReadableDatabase crashing, onCreate not being called

在此代码中,DatabaseHandler不执行onCreate()如何? 这个原始列表应用程序运行良好,我正处于尝试向应用程序中注入数据库的初期阶段,因此列表显示记录而不是编码字符串。

Android Intellisense显示了方法的可用性,但应用程序在运行时崩溃。 我怀疑onCreate()永远不会被调用,因为Log.d消息永远不会出现在logcat中。 我尝试了数据库实例化或上下文传递的许多组合都无济于事。

我正在使用adb logcat> lr.txt从电话中提取logcat,因为我无法运行仿真器,因为开发计算机是AMD而不是Intel处理器。

这是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.
 */

这是Mainactivity方法:TableMainLayout是显示该列表的下一个活动。 我从codeofaninja.com收到了原始代码,并正在向其中添加数据库。 当我注释掉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);

这是logcat:

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

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

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

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

D / AndroidRuntime(5891):关闭VM

应用程序在手机上崩溃:

Application LoadRunner(进程com.example.tablefreezepane)已意外停止。 请再试一次。

发现问题!:this.tvstatus.setText(“表已创建...”); 一直以来都是活动问题。 我不认为我在屏幕上显示该表格。 我重新整理了一下,应用程序继续运行。 所以现在我可以解决2个问题。 清理显示并开始调查创建的表。 它在电话上。 谢谢@Jemshit的指导。 我会再请您提供。 哦,今天真好! -

我看不到您获得了textview的引用。 因此,当您调用.settext时,tvstatus没有指向任何内容。 这导致nopointerexception ..使用findviewbyid首先获取引用。

暂无
暂无

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

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