简体   繁体   English

getReadableDatabase()上的SQLiteAssetHelper NullPointerException

[英]SQLiteAssetHelper NullPointerException at getReadableDatabase()

Because @CommonsWare suggested (in the comments to this answer ) using the Android SQLiteAssetHelper library, I decided not to use Using your own SQLite database in Android applications method (a popular SO answer) to copy my pre-populated database from my assets folder to the app database folder. 因为@CommonsWare建议(在此答案的注释中)使用Android SQLiteAssetHelper库,所以我决定不使用在Android应用程序中使用自己的SQLite数据库方法(一个流行的SO答案)将预先填充的数据库从资产文件夹复制到应用程序数据库文件夹。

Following the Android SQLiteAssetHelper directions , I set up my project like this: 按照Android SQLiteAssetHelper的指示进行操作 ,我按如下方式设置项目:

Since I am using gradle, my database was in src/main/assets/databases/test.db.zip . 由于我使用gradle,因此我的数据库位于src/main/assets/databases/test.db.zip

I used the .zip extension because the directions said 我使用了.zip扩展名,因为指导说明

Earlier versions of this library required the database asset to be compressed within a ZIP archive. 该库的早期版本要求将数据库资产压缩在ZIP存档中。 This is no longer a requirement, but is still supported. 这不再是必需的,但仍受支持。 Applications still targeting Gingerbread (API 10) or lower should continue to provide a compressed archive to ensure large database files are not corrupted during the packaging process. 仍以Gingerbread(API 10)或更低版本为目标的应用程序应继续提供压缩存档,以确保大型数据库文件在打包过程中不会损坏。

and I want to support earlier versions of android. 并且我想支持早期版本的android。

My database class is similar to the following: 我的数据库类类似于以下内容:

public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "test.db.zip";
    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
}

Again, I used the .zip extension for the DATABASE_NAME value because the directions said 同样,我将.zip扩展名用作DATABASE_NAME值,因为说明中指出

...you must provide...a SQLite database inside the databases folder whose file name matches the database name you provide in code (including the file extension, if any) ...您必须在数据库文件夹中提供一个SQLite数据库,其文件名与您在代码中提供的数据库名匹配(包括文件扩展名,如果有的话)

However, when I do SQLiteDatabase db = getReadableDatabase(); 但是,当我执行SQLiteDatabase db = getReadableDatabase(); I get a NullPointerException. 我得到了NullPointerException。 What is the problem? 问题是什么?

These SO questions and answers are not the same issue: 这些SO问题和答案不是相同的问题:

I set up the question to make the answer somewhat obvious, but here it is: 我设置了一个问题,使答案有些明显,但这是:

Contrary to what the Android SQLiteAssetHelper directions make it sound like (at the time of this writing, anyway. hopefully they will clarity them in the future), the DATABASE_NAME value should not include the .zip extension. 与Android SQLiteAssetHelper指示的含义相反(无论如何,在撰写本文时。希望它们将来能使它们变得清晰), DATABASE_NAME值不应包含.zip扩展名。 Change your code to 将您的代码更改为

 private static final String DATABASE_NAME = "test.db"; // no .zip extension

but keep src/main/assets/databases/test.db.zip as it is, and everything should work. 但请保持src/main/assets/databases/test.db.zip原样,一切正常。

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

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