在甚至从assets文件夹复制数据库之前,SQLiteAssetHelper会导致即时崩溃

时间:2014-02-17 02:01:52

标签: java android sqlite

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

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

使用先前的onCreate()方法继承我的代码,该方法有效,留在评论中。之前工作的唯一其他变化是SQLiteOpenHelper而不是SQLiteAssetHelper。我的数据库位于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);会阻止它崩溃

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

我删除了一堆不相干的东西。其余大部分也很可能无关紧要。感谢

我的应用程序定位4.0+,因此无需Zip。

1 个答案:

答案 0 :(得分:1)

不要初始化在其声明中需要Context的Activity实例变量,因为Context将为null。在dbTools

等适当的生命周期方法中初始化onCreate
DbHelper dbTools;

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