SQLiteAssetHelper导致立即崩溃,甚至没有从资产文件夹复制数据库 [英] SQLiteAssetHelper causes instant crash before even copying the database from the assets folder

查看:97
本文介绍了SQLiteAssetHelper导致立即崩溃,甚至没有从资产文件夹复制数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://github.com/jgilfelt/android-sqlite-asset-helper

我正在从 SQLiteOpenHelper 切换到 SQLiteAssetHelper ,因此我可以使用存储在/assests文件夹中的现有数据库.我的代码非常基础,通常它会创建一个表数据库,并且可以将数据插入其中并从中检索数据.我切换到 SQLiteAssetHelper ,摆脱了 onCreate 方法,因为它现在应该只使用已经创建的方法,然后再次运行该代码,一旦我开始它就会崩溃该活动创建了我的 SQLiteAssetHelper 帮助器类的对象,这使我认为该帮助器类存在问题,还因为似乎没有数据库被复制到我的应用程序/data/data/packagename文件夹中.

I'm switching from SQLiteOpenHelper to SQLiteAssetHelper so i can use an existing DB stored in the /assests folder. My code is very basic, normally it creates a one table DB and can insert data into it and retrieve data from it. I switched to SQLiteAssetHelper, got rid of the onCreate method as it should now just use an already created one, and then ran the code again and it crashes as soon as i start the activity which creates an object of my SQLiteAssetHelper helper class, which makes me think the helper class has the problem, also because no database appears to have been copied to my app /data/data/packagename folder.

使用以前的 onCreate()方法(在此方法中起作用)在注释中保留我的代码.以前起作用的唯一其他变化是扩展了 SQLiteOpenHelper 而不是 SQLiteAssetHelper .我的数据库在 sourceprojectfolder/assests/databases/productsdb.db

Heres my code with the previous onCreate() method, whiched worked, left in comments. The only other change from it previously working is extending SQLiteOpenHelper instead of SQLiteAssetHelper. My database is in sourceprojectfolder/assests/databases/productsdb.db

帮助程序类

package com.example.myfirstapp;

 import java.util.HashMap;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

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

class DbHelper extends SQLiteAssetHelper{

    public static final String KEY_ROWID = "id";
    public static final String KEY_NAME = "product_name";
    public static final String KEY_DESC = "product_description";

    private static final String DATABASE_NAME = "productsdb.db";
    private static final String DATABASE_TABLE = "products";
    private static final int DATABASE_VERSION = 1;


    public DbHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub

    }

/* //this method isn't needed with SQLiteAssetHelper it says. Thats why its commented.
    @Override
    public void onCreate(SQLiteDatabase db) {


        String query = "CREATE TABLE products ( productId INTEGER PRIMARY KEY, productName TEXT, " +
                "productDesc TEXT)";

        // Executes the query provided as long as the query isn't a select
        // or if the query doesn't return any data

        db.execSQL(query);
    }
*/

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

//insert info method
    public void insertContact(HashMap<String, String> queryValues) {

    ...

    }

//get info method
      public HashMap<String, String> getInfo(String id) {

     ...

       }


}

该课程立即崩溃.使用帮助程序类在数据库中放置数据或从中检索数据的类.我有一个启动的菜单类,然后当我单击按钮启动该类时,它崩溃了,删除 DbHelper dbTools = new DbHelper(this); 阻止它崩溃

This class crashes instantly. Class which uses the helper class to put and retrieve data to/from the Database. I have a menu class which launches, then when i click the button to launch this class it crashes, removing DbHelper dbTools = new DbHelper(this); stops it from crashing

public class ScanActivity extends Activity {

    TextView textview;
    TextView productNameTV;
    Button scanB;
    Button bGetInfo;

    EditText etId, etName, etDesc;
    Button bInsert;

    DbHelper dbTools = new DbHelper(this);


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

        Bundle extras = getIntent().getExtras();

        String datas= extras.getString("EXTRA_ID");

        productNameTV = (TextView) findViewById(R.id.textView42);
        textview = (TextView) findViewById(R.id.textView41);
        textview.setText(datas);


        etId = (EditText) findViewById(R.id.etId);
        etName = (EditText) findViewById(R.id.etName);
        etDesc = (EditText) findViewById(R.id.etDesc);

        bInsert = (Button) findViewById(R.id.bInsert);
        bInsert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                insertProduct();
            }
        });


        bGetInfo = (Button) findViewById(R.id.bGetInfo);
        bGetInfo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                getInfo();
            }
        });


        scanB = (Button) findViewById(R.id.button1);
        scanB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                sendMessage(v);
            }
        });
    }


    public void insertProduct(){

        HashMap<String, String> queryValuesMap =  new  HashMap<String, String>();


        queryValuesMap.put("productId", etId.getText().toString());
        queryValuesMap.put("productName", etName.getText().toString());
        queryValuesMap.put("productDesc", etDesc.getText().toString());

        dbTools.insertContact(queryValuesMap);

    }


 public void getInfo(){

     String productId = etId.getText().toString();
     HashMap<String, String> contactList = dbTools.getInfo(productId);

        // Make sure there is something in the contactList

        if(contactList.size()!=0) {

            // Put the values in the EditText boxes

            etId.setText(contactList.get("productId"));
            etName.setText(contactList.get("productName"));
            etDesc.setText(contactList.get("productDesc"));
            //emailAddress.setText(contactList.get("emailAddress"));

        }

 } 


}

LogCat

02-17 01:47:22.521: D/AndroidRuntime(5129): Shutting down VM
02-17 
01:47:22.521: W/dalvikvm(5129): threadid=1: thread exiting with uncaught exception (group=0x4179eba8)
02-17 
01:47:22.531: E/AndroidRuntime(5129): FATAL EXCEPTION: main
02-17 01:47:22.531: E/AndroidRuntime(5129): Process: com.example.myfirstapp, PID: 5129
02-17 
01:47:22.531: E/AndroidRuntime(5129): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.ScanActivity}: java.lang.NullPointerException
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.os.Looper.loop(Looper.java:136)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.app.ActivityThread.main(ActivityThread.java:5017)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at java.lang.reflect.Method.invokeNative(Native Method)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at java.lang.reflect.Method.invoke(Method.java:515)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at dalvik.system.NativeStart.main(Native Method)
02-17 
01:47:22.531: E/AndroidRuntime(5129): Caused by: java.lang.NullPointerException
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init>(SQLiteAssetHelper.java:109)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init>(SQLiteAssetHelper.java:129)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at com.example.myfirstapp.DbHelper.<init>(DbHelper.java:28)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at com.example.myfirstapp.ScanActivity.<init>(ScanActivity.java:24)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at java.lang.Class.newInstanceImpl(Native Method)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at java.lang.Class.newInstance(Class.java:1208)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-17 
01:47:22.531: E/AndroidRuntime(5129):   ... 12 more

我删除了一堆无关紧要的东西.其余大多数也很可能是不相关的.谢谢

I removed a bunch of irrelevant things. Most of the rest is most likely irrelevant too. Thanks

我的应用定位到4.0+,因此无需压缩.

My app targets 4.0+ so there is no need to Zip.

推荐答案

请勿初始化在其声明中需要Context的Activity实例变量,因为Context将为null.在适当的生命周期方法(例如 onCreate :

Do not initialize Activity instance variables that require a Context in their declarations, as the Context will be null. Initialize dbTools inside an appropriate lifecycle method like onCreate:

DbHelper dbTools;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    dbTools = new DbHelper(this);
}

这篇关于SQLiteAssetHelper导致立即崩溃,甚至没有从资产文件夹复制数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆