获取使用getAssets资产的文件()始终失败 [英] Getting file from assets using getAssets() consistently fails

查看:755
本文介绍了获取使用getAssets资产的文件()始终失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code我在下面列举的方法 getPieceTextFile()应该返回的InputStream 来读取指定的文件数据。然而,这种方法总是下降的渔获物和使返回null,从而导致NPE。

我列出了LogCat中输出和code下面的类。任何标识问题的潜在来源是美联社preciated。

LogCat中:

  12月1日至3日:17:51.331:I / dalvikvm(589):主题ID = 3:反应信号3
十二月1日至3日:17:51.590:I / dalvikvm(589):写的堆栈跟踪到/data/anr/traces.txt
十二月1日至3日:17:51.831:I / dalvikvm(589):主题ID = 3:反应信号3
十二月1日至3日:17:51.871:I / dalvikvm(589):写的堆栈跟踪到/data/anr/traces.txt
十二月1日至3日:17:55.081:我/的System.out(589):资产管理器/是不行的。
十二月1日至3日:17:55.081:W / System.err的(589):显示java.lang.NullPointerException
十二月1日至3日:17:55.091:W / System.err的(589):在com.lawson.englishlitrevision.ManipulateDatabase.getPieceTextFile(ManipulateDatabase.java:387)
十二月1日至3日:17:55.091:W / System.err的(589):在com.lawson.englishlitrevision.ManipulateDatabase<&初始化GT;(ManipulateDatabase.java:398)
十二月1日至3日:17:55.091:W / System.err的(589):在com.lawson.englishlitrevision.Novel.onCreate(Novel.java:22)
十二月1日至3日:17:55.091:W / System.err的(589):在android.app.Activity.performCreate(Activity.java:4465)
十二月1日至3日:17:55.091:W / System.err的(589):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
十二月1日至3日:17:55.091:W / System.err的(589):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
十二月1日至3日:17:55.091:W / System.err的(589):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
十二月1日至3日:17:55.091:W / System.err的(589):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
十二月1日至3日:17:55.091:W / System.err的(589):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
十二月1日至3日:17:55.091:W / System.err的(589):在android.os.Handler.dispatchMessage(Handler.java:99)
十二月1日至3日:17:55.091:W / System.err的(589):在android.os.Looper.loop(Looper.java:137)
十二月1日至3日:17:55.101:W / System.err的(589):在android.app.ActivityThread.main(ActivityThread.java:4424)
十二月1日至3日:17:55.101:W / System.err的(589):在java.lang.reflect.Method.invokeNative(本机方法)
十二月1日至3日:17:55.101:W / System.err的(589):在java.lang.reflect.Method.invoke(Method.java:511)
十二月1日至3日:17:55.101:W / System.err的(589):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
十二月1日至3日:17:55.101:W / System.err的(589):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
十二月1日至3日:17:55.101:W / System.err的(589):在dalvik.system.NativeStart.main(本机方法)
十二月1日至3日:17:55.101:W / dalvikvm(589):主题ID = 1:螺纹未捕获的异常(组= 0x409c01f8)退出
十二月1日至3日:17:55.121:E / AndroidRuntime(589):致命异常:主要
十二月1日至3日:17:55.121:E / AndroidRuntime(589):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.lawson.englishlitrevision / com.lawson.englishlitrevision.Novel}:显示java.lang.NullPointerException
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.os.Handler.dispatchMessage(Handler.java:99)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.os.Looper.loop(Looper.java:137)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.ActivityThread.main(ActivityThread.java:4424)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在java.lang.reflect.Method.invokeNative(本机方法)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在java.lang.reflect.Method.invoke(Method.java:511)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在dalvik.system.NativeStart.main(本机方法)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):致:显示java.lang.NullPointerException
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在java.io.Reader中的<&初始化GT;(Reader.java:64)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在java.io.InputStreamReader中的<&初始化GT;(InputStreamReader.java:79)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在com.lawson.englishlitrevision.ManipulateDatabase<&初始化GT;(ManipulateDatabase.java:398)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在com.lawson.englishlitrevision.Novel.onCreate(Novel.java:22)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.Activity.performCreate(Activity.java:4465)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
十二月1日至3日:17:55.121:E / AndroidRuntime(589):11 ...更多
十二月1日至3日:17:55.572:I / dalvikvm(589):主题ID = 3:反应信号3
十二月1日至3日:17:55.580:I / dalvikvm(589):写的堆栈跟踪到/data/anr/traces.txt
十二月1日至3日:17:55.681:I / dalvikvm(589):主题ID = 3:反应信号3
十二月1日至3日:17:55.690:I / dalvikvm(589):写的堆栈跟踪到/data/anr/traces.txt

类code:

  / **
 *包含SQL语句和管理连接
 *
 * @author丹尼尔·劳森
 * @version 29/12/13
 * /
包com.lawson.englishlitrevision;进口android.app.Activity;
进口android.content.ContentValues​​;
进口android.content.Context;
进口android.content.res.AssetManager;进口的java.util.ArrayList;
进口java.util.Scanner中;
进口的java.io.File;
进口java.io.FileReader;
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteException;
进口android.database.sqlite.SQLiteOpenHelper;公共类ManipulateDatabase {    / **字符串重新presenting数据库的名称* /
    私有静态最后弦乐的databaseName =EnglishDatabase.db;    / **重新诠释presenting数据库*当前版本/
    私有静态最终诠释databaseVersion = 1;    // ------------------------------------------------ ----------------------------------    / **
     *串重新presenting存储该块的名称表并
     *是否是播放或没有。
     * /
    私有静态最后弦乐pieceTable =pieceTable;    / **
     *串重新presenting存储该点所作的名称表,
     *它所属的一块,它涉及到哪些元素。
     * /    私有静态最后弦乐specificTable =specificTable;    / **
     *串重新presenting存储该元件的表中,一块其
     *属于,和它是否是一个字符。
     * /    私有静态最后弦乐elementTable =elementTable;    / **
     *字符串重新presenting存储报价和点它表
     *链接。
     * /    私有静态最后弦乐quoteTable =quoteTable;    / **
     *字符串重新presenting存储在片和表
     *在不同日期正确/不正确的反应
     * /    私有静态最后弦乐statsTable =statsTable;    // ------------------------------------------------ ----------------------------------    / **字符串重新presenting这块表的一块领域* /    私有静态最后弦乐pieceField =作品;    / **字符串重新presenting这块表的类型字段* /    私有静态最后弦乐TYPEFIELD =isPlay;    / **字符串重新presenting的特定表的角度来看领域* /    私有静态最后弦乐pointField =点;    / **字符串重新presenting元素表的元素字段* /    私有静态最后弦乐elementField =元素;    / **字符串重新presenting单元表的elType领域* /    私有静态最后弦乐elTypeField =isCharacter;    / **字符串重新presenting报价表的报价领域* /    私有静态最后弦乐quoteField =引用;    / **字符串重新presenting的统计数据表格*的ID字段/    私有静态最后弦乐idField =statId;    / **字符串重新presenting的统计数据表格*正确的领域/    私有静态最后弦乐correctField =正确的;    / **字符串重新presenting的统计数据表格*的不正确的字段/    私有静态最后弦乐incorrectField =不正确;    / **字符串重新presenting的统计数据表格*的日期字段/    私有静态最后弦乐的DateField =日期;    // ------------------------------------------------ ----------------------------------    / ** String数组重新presenting这块表的所有字段* /    私有静态最后的String [] = pieceTableFields {pieceField,TYPEFIELD};    / ** String数组重新presenting特定表的所有字段* /    私有静态最后的String [] = specificTableFields {pointField,
            pieceField,elementField};    / ** String数组重新presenting报价表的所有字段* /    私有静态最后的String [] = quoteTableFields {quoteField,pointField};    / ** String数组重新presenting元素表的所有字段* /    私有静态最后的String [] = elementTableFields {elementField,
            pieceField,elTypeField};    / ** String数组重新presenting的统计数据表格*的各个领域/    私有静态最后的String [] = statsTableFields {idField,pieceField,
            correctField,incorrectField,的DateField};    // ------------------------------------------------ ----------------------------------    / ** SQL语句创建一块表。 * /    私有静态最后弦乐createPieceTable =CREATE TABLE+ pieceTable
            +(+ pieceField +TEXT NOT NULL PRIMARY KEY,+ TYPEFIELD
            +INTEGER);;    / ** SQL语句来创建特定的表。 * /    私有静态最后弦乐createSpecificTable =CREATE TABLE
            + specificTable +(+ pointField +TEXT NOT NULL PRIMARY KEY
            + pieceField +TEXT NOT NULL,+ elementField +TEXT NOT NULL);    / ** SQL语句来创建的报价表。 * /    私有静态最后弦乐createQuoteTable =CREATE TABLE+ quoteTable
            +(+ quoteField +TEXT NOT NULL PRIMARY KEY,+ pointField
            +TEXT NOT NULL PRIMARY KEY);    / ** SQL语句来创建单元表。 * /    私有静态最后弦乐createElementTable =CREATE TABLE
            + elementTable +(+ elementField +TEXT NOT NULL PRIMARY KEY
            + pieceField +TEXT NOT NULL,+ elTypeField +INTEGER);    / ** SQL语句创建的统计数据表格。 * /    私有静态最后弦乐createStatsTable =CREATE TABLE+ statsTable
            +(+ idField +INTEGER PRIMARY KEY,+ pieceField
            +TEXT NOT NULL,+ correctField +INTEGER,+ incorrectField
            +INTEGER,+ +的DateFieldDATE);    // ------------------------------------------------ ----------------------------------    / **
     * SQL语句来搜索这块表 - 替换[COLUMNNAME]和
     * [值]与现场进行搜索和寻找什么价值。
     * /    私有静态最后弦乐searchPieceTable =SELECT * FROM
            + pieceTable +WHERE [COLUMNNAME] ='[值]';;    / **
     * SQL语句来搜索特定的表 - 替换[COLUMNNAME]和
     * [值]与现场进行搜索和寻找什么价值。
     * /    私有静态最后弦乐searchSpecificTable =SELECT * FROM
            + specificTable +WHERE [COLUMNNAME] ='[值]'。;    / **
     * SQL语句搜索报价表 - 替换[COLUMNNAME]和
     * [值]与现场进行搜索和寻找什么价值。
     * /    私有静态最后弦乐searchQuoteTable =SELECT * FROM
            + quoteTable +WHERE [COLUMNNAME] ='[值]'。;    / **
     * SQL语句搜索元素表 - 替换[COLUMNNAME]和
     * [值]与现场进行搜索和寻找什么价值。
     * /    私有静态最后弦乐searchElementTable =SELECT * FROM
            + elementTable +WHERE [COLUMNNAME] ='[值]'。;    / **
     * SQL语句搜索统计数据表格 - 替换[COLUMNNAME]和
     * [值]与现场进行搜索和寻找什么价值。
     * /    私有静态最后弦乐searchStatsTable =SELECT * FROM
            + statsTable +WHERE [COLUMNNAME] ='[值]'。;    // ------------------------------------------------ ----------------------------------    / **
     * SQL语句更新这块表 - 替换[assignValues​​]和
     * [状态]的项目要改变+他们新的价值观和条件
     *,如果确实如此,这种变化应适用。
     * /    私有静态最后弦乐updatePieceTable =UPDATE+ pieceTable
            +SET [assignValues​​],其中[状态];    / **
     * SQL语句来更新特定表 - 替换[assignValues​​]和
     * [状态]的项目要改变+他们新的价值观和条件
     *,如果确实如此,这种变化应适用。
     * /    私有静态最后弦乐updateSpecificTable =UPDATE+ specificTable
            +SET [assignValues​​],其中[状态];    / **
     * SQL语句更新报价表 - 替换[assignValues​​]和
     * [状态]的项目要改变+他们新的价值观和条件
     *,如果确实如此,这种变化应适用。
     * /    私有静态最后弦乐updateQuoteTable =UPDATE+ quoteTable
            +SET [assignValues​​],其中[状态];    / **
     * SQL语句更新单元表 - 替换[assignValues​​]和
     * [状态]的项目要改变+他们新的价值观和条件
     *,如果确实如此,这种变化应适用。
     * /    私有静态最后弦乐updateElementTable =UPDATE+ elementTable
            +SET [assignValues​​],其中[状态];    / **
     * SQL语句来更新统计数据表格 - 替换[assignValues​​]和
     * [状态]的项目要改变+他们新的价值观和条件
     *,如果确实如此,这种变化应适用。
     * /    私有静态最后弦乐updateStatsTable =UPDATE+ statsTable
            +SET [assignValues​​],其中[状态];    // ------------------------------------------------ ----------------------------------    / **
     * SQL语句从一张表中删除 - 替换[状态]的
     *条件,如果确实如此,这应该删除应用。
     * /    私有静态最后弦乐deleteFromPieceTable =DELETE FROM
            + pieceTable +WHERE [状态];    / **
     * SQL语句从特定的表中删除 - 替换[状态]的
     *条件,如果确实如此,这应该删除应用。
     * /    私有静态最后弦乐deleteFromSpecificTable =DELETE FROM
            + specificTable +WHERE [状态];    / **
     * SQL语句从报价表中删除 - 替换[状态]的
     *条件,如果确实如此,这应该删除应用。
     * /    私有静态最后弦乐deleteFromQuoteTable =DELETE FROM
            + quoteTable +WHERE [状态];    / **
     * SQL语句从元素表中删除 - 替换[状态]的
     *条件,如果确实如此,这应该删除应用。
     * /    私有静态最后弦乐deleteFromElementTable =DELETE FROM
            + elementTable +WHERE [状态];    / **
     * SQL语句从统计数据表中删除 - 替换[状态]的
     *条件,如果确实如此,这应该删除应用。
     * /    私有静态最后弦乐deleteFromStatsTable =DELETE FROM
            + statsTable +WHERE [状态];    // ------------------------------------------------ ----------------------------------    / **
     * SQL语句来插入一张表 - 与替换[值]
     要插入*值。
     * /    私有静态最后弦乐insertIntoPieceTable =INSERT INTO
            + pieceTable +VALUES([值]);    / **
     * SQL语句插入到特定表 - 其替换[值]
     *将被插入的值。
     * /    私有静态最后弦乐insertIntoSpecificTable =INSERT INTO
            + specificTable +VALUES([值]);    / **
     * SQL语句插入到表报价 - 与替换[值]
     要插入*值。
     * /    私有静态最后弦乐insertIntoQuoteTable =INSERT INTO
            + quoteTable +VALUES([值]);    / **
     * SQL语句来插入元素表 - 其替换[值]
     *将被插入的值。
     * /    私有静态最后弦乐insertIntoElementTable =INSERT INTO
            + elementTable +VALUES([值]);    / **
     * SQL语句插入到表报价 - 与替换[值]
     要插入*值。
     * /    私有静态最后弦乐insertIntoStatsTable =INSERT INTO
            + statsTable +VALUES([值]);    // ------------------------------------------------ ----------------------------------    / **
     * SQLiteDatabase重新presenting英文数据库
     * /    私人SQLiteDatabase数据库;    / **
     *利用'访问'数据库DatabaseHelper类的实例
     * /    私人DatabaseHelper baseHelper;    / **
     *数据库机械手的内容(ManipulateDatabase类)
     * /    私人活动范围内;    / **
     *布尔存储是否这是第一次在此基础/版本
     *已经启动
     * /    私人布尔initialLaunch = FALSE;    / **
     *文件中的BufferedReader读入数据库
     * /    私人文件pieceList;    / **
     *方法从资产获得pieceText文件的BufferedReader使用
     *
     *引发IOException
     * /    私人的InputStream getPieceTextFile(上下文CTX)抛出IOException        尝试{
            AssetManager经理= ctx.getAssets();
            InputStream为= manager.open(pieceText.txt);
            回报;
        }赶上(例外五){
            的System.out.println(资产管理器/是不行的。);
            e.printStackTrace();
            返回null;
        }
    }    私人的BufferedReader pieceScan =新的BufferedReader(
            新的InputStreamReader(getPieceTextFile(this.context),UTF-8));    / *
     *私人文件specificList =新的文件(specificList.txt);
     *
     *私人的BufferedReader specificScan =新的BufferedReader(新的FileReader(
     *specificList.txt));
     *
     *私人文件元素列表=新的文件(elementList.txt);
     *
     *私人的BufferedReader elementScan =新的BufferedReader(新的FileReader(
     *elementList.txt));
     *
     *私人文件quoteList =新的文件(quoteList.txt);
     *
     *私人的BufferedReader quoteScan =新的BufferedReader(新的FileReader(
     *quoteList.txt));
     * /
    / **
     *方法打开数据库
     * /    私人无效OPENSESAME(){
        尝试{
            数据库= baseHelper.getWritableDatabase();
        }赶上(SQLiteException E){
            的System.out.println(数据库无法打开);
            e.printStackTrace();
        }
    }    / **
     *方法关闭数据库
     * /    私人无效closeSesame(){
        baseHelper.close();
    }    公众的ArrayList<串GT; getPieces(字符串typePiece){
        尝试{
            芝麻开门();
            ArrayList的<串GT;件=新的ArrayList<串GT;();
            字符串命令= searchPieceTable
                    .replace([COLUMNNAME],TYPEFIELD);
            命令= command.replace([值],typePiece);            光标光标= dataBase.rawQuery(指挥,NULL);
            cursor.moveToFirst();
            而(!cursor.isAfterLast()){
                pieces.add(cursor.getString(cursor.getColumnIndex(pieceField)));
                cursor.moveToNext();
            }
            closeSesame();
            返回件;
        }赶上(SQLiteException E){
            e.printStackTrace();
            的System.out.println(SQL问题);
            ArrayList的<串GT; defaultReturn =新的ArrayList<串GT;();
            defaultReturn.add(有是默认的项目。);
            defaultReturn.add(因为适当的ArrayList);
            defaultReturn.add(未正确填写);
            返回defaultReturn;
        }
    }    公众的ArrayList<串GT; getAllPieces(){        尝试{
            芝麻开门();
            ArrayList的<串GT; allPieces =新的ArrayList<串GT;();
            字符串指令=SELECT * FROM pieceTable            光标光标= dataBase.rawQuery(指令,NULL);
            cursor.moveToFirst();
            而(!cursor.isAfterLast()){
                allPieces.add(cursor.getString(光标
                        .getColumnIndex(pieceField)));
                cursor.moveToNext();
            }            closeSesame();
            返回allPieces;
        }赶上(例外五){
            e.printStackTrace();
            ArrayList的<串GT; defaultList =新的ArrayList<串GT;();
            defaultList.add(抓默认);
            返回defaultList;
        }    }    / **
     *构造
     *
     * @参数contextIn
     *在MainpuxlateDatabase类的语境
     *引发IOException
     * /    公共ManipulateDatabase(活动contextIn)抛出IOException
        baseHelper =新DatabaseHelper(contextIn);
        上下文= contextIn;
        芝麻开门();
        串线;
        字符串x;
        串pieceName;
        串isDrama;
        串点;
        字符串元素;
        串报价;
        串isCharacter;
        INT commaPoint;
        INT commaPoint2;        如果(真){
            而((X = pieceScan.readLine())!= NULL){
                行= pieceScan.readLine();
                的System.out.println(线);
                commaPoint = line.indexOf(,);
                pieceName = line.substring(0,commaPoint);
                isDrama = line.substring(commaPoint + 1,line.length() - 1);
                ContentValues​​ PV =新ContentValues​​();
                pv.put(pieceField,pieceName);
                pv.put(TYPEFIELD,isDrama);
                dataBase.insert(pieceTable,空,PV);                的System.out.println(你这该死的鲁钝白痴!);
            }            / *
             而*((X = specificScan.readLine())!= NULL){行=
             * specificScan.readLine();的System.out.println(线); commaPoint =
             * line.indexOf(,);点= line.substring(0,commaPoint);
             * commaPoint2 =(线.substring(commaPoint + 1,line.length() - 1)
             * 。指数(,)); pieceName = line.substring(commaPoint + 1,
             * commaPoint2);元素= line.substring(commaPoint2 + 1,
             * line.length() - 1);
             *
             * ContentValues​​ SV =新ContentValues​​(); sv.put(pointField,
             *点); sv.put(pieceField,pieceName); sv.put(elementField,
             *元件); dataBase.insert(specificTable,空,SV); }而((X =
             * elementScan.readLine())= NULL){行= elementScan.readLine()!;
             *的System.out.println(线); commaPoint = line.indexOf(,);元件
             * = line.substring(0,commaPoint); commaPoint2 =(行
             * .substring(commaPoint + 1,line.length() - 1).indexOf(,));
             * pieceName = line.substring(commaPoint + 1,commaPoint2);
             * isCharacter =行.substring(commaPoint2 + 1,line.length() -
             * 1);
             *
             * ContentValues​​ EV =新ContentValues​​(); ev.put(elementField,
             *元件); ev.put(pieceField,pieceName); ev.put(elTypeField,
             * isCharacter); dataBase.insert(elementTable,空,EV);
             *
             *},而((X = quoteScan.readLine())!= NULL){行=
             * quoteScan.readLine();的System.out.println(线); commaPoint =
             * line.indexOf(,);报价= line.substring(0,commaPoint);点=
             * line.substring(commaPoint + 1,line.length() - 1);
             *
             * ContentValues​​ QV =新ContentValues​​(); qv.put(quoteField,
             *报价); qv.put(pointField,点); dataBase.insert(quoteTable,
             *空,QV); }
             * /        }    }    // ------------------------------------------------ ----------------------------------    / **
     *数据库帮手 - 三种基本方法 - 所需的数据库任务
     *
     * @author丹尼尔·劳森
     * @version 30/12/13
     * /    私有类DatabaseHelper扩展SQLiteOpenHelper {        私人DatabaseHelper(上下文theContext){
            超(theContext,的databaseName,空,databaseVersion);
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB){
            尝试{
                db.execSQL(createPieceTable);
                / *
                 * db.execSQL(createSpecificTable);
                 * db.execSQL(createQuoteTable); db.execSQL(createElementTable);
                 * db.execSQL(createStatsTable);
                 * /
                initialLaunch = TRUE;            }赶上(SQLiteException E){
                的System.out.println(无法创建表(S));
                e.printStackTrace();
            }        }        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
            db.execSQL(DROP TABLE IF EXISTS+ pieceTable);
            db.execSQL(DROP TABLE IF EXISTS+ specificTable);
            db.execSQL(DROP TABLE IF EXISTS+ quoteTable);
            db.execSQL(DROP TABLE IF EXISTS+ elementTable);
            db.execSQL(DROP TABLE IF EXISTS+ statsTable);
            的onCreate(DB);        }    }
}


解决方案

 私人的BufferedReader pieceScan =新的BufferedReader(
        新的InputStreamReader(getPieceTextFile(this.context),UTF-8));

这初始化code运行时,你的类实例化。 this.context 。这显示在logcat中在

  12月1日至3日:17:55.091:W / System.err的(589):在com.lawson.englishlitrevision.ManipulateDatabase.getPieceTextFile(ManipulateDatabase.java:387)
十二月1日至3日:17:55.091:W / System.err的(589):在com.lawson.englishlitrevision.ManipulateDatabase<&初始化GT;(ManipulateDatabase.java:398)

其中,<&初始化GT; 指的构造函数和成员变量的初始化。您初始化构造背景,但构造唯一的成员变量初始化后运行。

就移动 pieceScan 初始化构造。

In the code I have listed below, the method getPieceTextFile() should return the InputStream to read data from the specified file. However, the method always falls back on the catch and so returns null, leading to a NPE.

I have listed the LogCat output and the code for the class below. Any identification of the potential source of the problem would be appreciated.

LogCat:

01-03 12:17:51.331: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:51.590: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'
01-03 12:17:51.831: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:51.871: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'
01-03 12:17:55.081: I/System.out(589): Asset Manager/IS not work.
01-03 12:17:55.081: W/System.err(589): java.lang.NullPointerException
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.getPieceTextFile(ManipulateDatabase.java:387)
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.<init>(ManipulateDatabase.java:398)
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.Novel.onCreate(Novel.java:22)
01-03 12:17:55.091: W/System.err(589):  at android.app.Activity.performCreate(Activity.java:4465)
01-03 12:17:55.091: W/System.err(589):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-03 12:17:55.091: W/System.err(589):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 12:17:55.091: W/System.err(589):  at android.os.Looper.loop(Looper.java:137)
01-03 12:17:55.101: W/System.err(589):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-03 12:17:55.101: W/System.err(589):  at java.lang.reflect.Method.invokeNative(Native Method)
01-03 12:17:55.101: W/System.err(589):  at java.lang.reflect.Method.invoke(Method.java:511)
01-03 12:17:55.101: W/System.err(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 12:17:55.101: W/System.err(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 12:17:55.101: W/System.err(589):  at dalvik.system.NativeStart.main(Native Method)
01-03 12:17:55.101: W/dalvikvm(589): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-03 12:17:55.121: E/AndroidRuntime(589): FATAL EXCEPTION: main
01-03 12:17:55.121: E/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lawson.englishlitrevision/com.lawson.englishlitrevision.Novel}: java.lang.NullPointerException
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.os.Looper.loop(Looper.java:137)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.lang.reflect.Method.invokeNative(Native Method)
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.lang.reflect.Method.invoke(Method.java:511)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 12:17:55.121: E/AndroidRuntime(589):  at dalvik.system.NativeStart.main(Native Method)
01-03 12:17:55.121: E/AndroidRuntime(589): Caused by: java.lang.NullPointerException
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.io.Reader.<init>(Reader.java:64)
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.io.InputStreamReader.<init>(InputStreamReader.java:79)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.lawson.englishlitrevision.ManipulateDatabase.<init>(ManipulateDatabase.java:398)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.lawson.englishlitrevision.Novel.onCreate(Novel.java:22)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.Activity.performCreate(Activity.java:4465)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-03 12:17:55.121: E/AndroidRuntime(589):  ... 11 more
01-03 12:17:55.572: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:55.580: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'
01-03 12:17:55.681: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:55.690: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'

Class Code:

/**
 * Contains SQL statements and manages connections
 * 
 * @author Daniel Lawson
 * @version 29/12/13
 */
package com.lawson.englishlitrevision;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class ManipulateDatabase {

    /** String representing the name of the database */
    private static final String databaseName = "EnglishDatabase.db";

    /** Int representing the current version of the database */
    private static final int databaseVersion = 1;

    // ----------------------------------------------------------------------------------

    /**
     * String representing the table that stores the name of the piece and
     * whether it is a play or not.
     */
    private static final String pieceTable = "pieceTable";

    /**
     * String representing the table that stores the name of the point made,
     * which piece it belongs to, and which element it pertains to.
     */

    private static final String specificTable = "specificTable";

    /**
     * String representing the table that stores the element, the piece it
     * belongs to, and whether or not it is a character.
     */

    private static final String elementTable = "elementTable";

    /**
     * String representing the table that stores the quote and the point it
     * links to.
     */

    private static final String quoteTable = "quoteTable";

    /**
     * String representing the table that stores the piece and the
     * correct/incorrect responses on various dates
     */

    private static final String statsTable = "statsTable";

    // ----------------------------------------------------------------------------------

    /** String representing the piece field of the piece table */

    private static final String pieceField = "piece";

    /** String representing the type field of the piece table */

    private static final String typeField = "isPlay";

    /** String representing the point field of the specific table */

    private static final String pointField = "point";

    /** String representing the element field of the element table */

    private static final String elementField = "element";

    /** String representing the elType field of the element table */

    private static final String elTypeField = "isCharacter";

    /** String representing the quote field of the quote table */

    private static final String quoteField = "quote";

    /** String representing the ID field of the stats table */

    private static final String idField = "statId";

    /** String representing the correct field of the stats table */

    private static final String correctField = "correct";

    /** String representing the incorrect field of the stats table */

    private static final String incorrectField = "incorrect";

    /** String representing the date field of the stats table */

    private static final String dateField = "date";

    // ----------------------------------------------------------------------------------

    /** String array representing all the fields of the piece table */

    private static final String[] pieceTableFields = { pieceField, typeField };

    /** String array representing all the fields of the specific table */

    private static final String[] specificTableFields = { pointField,
            pieceField, elementField };

    /** String array representing all the fields of the quote table */

    private static final String[] quoteTableFields = { quoteField, pointField };

    /** String array representing all the fields of the element table */

    private static final String[] elementTableFields = { elementField,
            pieceField, elTypeField };

    /** String array representing all the fields of the stats table */

    private static final String[] statsTableFields = { idField, pieceField,
            correctField, incorrectField, dateField };

    // ----------------------------------------------------------------------------------

    /** SQL statement to create the piece table. */

    private static final String createPieceTable = "CREATE TABLE " + pieceTable
            + " (" + pieceField + " TEXT NOT NULL PRIMARY KEY, " + typeField
            + " INTEGER);";

    /** SQL statement to create the specific table. */

    private static final String createSpecificTable = "CREATE TABLE "
            + specificTable + "(" + pointField + " TEXT NOT NULL PRIMARY KEY,"
            + pieceField + " TEXT NOT NULL," + elementField + " TEXT NOT NULL)";

    /** SQL statement to create the quotes table. */

    private static final String createQuoteTable = "CREATE TABLE " + quoteTable
            + "(" + quoteField + " TEXT NOT NULL PRIMARY KEY," + pointField
            + " TEXT NOT NULL PRIMARY KEY)";

    /** SQL statement to create the element table. */

    private static final String createElementTable = "CREATE TABLE "
            + elementTable + "(" + elementField + " TEXT NOT NULL PRIMARY KEY,"
            + pieceField + " TEXT NOT NULL," + elTypeField + " INTEGER)";

    /** SQL statement to create the stats table. */

    private static final String createStatsTable = "CREATE TABLE " + statsTable
            + "(" + idField + " INTEGER PRIMARY KEY," + pieceField
            + " TEXT NOT NULL," + correctField + " INTEGER," + incorrectField
            + " INTEGER," + dateField + " DATE)";

    // ----------------------------------------------------------------------------------

    /**
     * SQL statement to search the piece table - replace [columnName] and
     * [value] with field to be searched and what value to look for.
     */

    private static final String searchPieceTable = "SELECT * FROM "
            + pieceTable + " WHERE [columnName] = '[value]';";

    /**
     * SQL statement to search the specific table - replace [columnName] and
     * [value] with field to be searched and what value to look for.
     */

    private static final String searchSpecificTable = "SELECT * FROM "
            + specificTable + " WHERE [columnName] = '[value]'";

    /**
     * SQL statement to search the quote table - replace [columnName] and
     * [value] with field to be searched and what value to look for.
     */

    private static final String searchQuoteTable = "SELECT * FROM "
            + quoteTable + " WHERE [columnName] = '[value]'";

    /**
     * SQL statement to search the element table - replace [columnName] and
     * [value] with field to be searched and what value to look for.
     */

    private static final String searchElementTable = "SELECT * FROM "
            + elementTable + " WHERE [columnName] = '[value]'";

    /**
     * SQL statement to search the stats table - replace [columnName] and
     * [value] with field to be searched and what value to look for.
     */

    private static final String searchStatsTable = "SELECT * FROM "
            + statsTable + " WHERE [columnName] = '[value]'";

    // ----------------------------------------------------------------------------------

    /**
     * SQL statement to update the piece table - replace [assignValues] and
     * [condition] with items to be altered + their new values and the condition
     * where if true, this change should be applied.
     */

    private static final String updatePieceTable = "UPDATE " + pieceTable
            + " SET [assignValues] WHERE [condition]";

    /**
     * SQL statement to update the specific table - replace [assignValues] and
     * [condition] with items to be altered + their new values and the condition
     * where if true, this change should be applied.
     */

    private static final String updateSpecificTable = "UPDATE " + specificTable
            + " SET [assignValues] WHERE [condition]";

    /**
     * SQL statement to update the quote table - replace [assignValues] and
     * [condition] with items to be altered + their new values and the condition
     * where if true, this change should be applied.
     */

    private static final String updateQuoteTable = "UPDATE " + quoteTable
            + " SET [assignValues] WHERE [condition]";

    /**
     * SQL statement to update the element table - replace [assignValues] and
     * [condition] with items to be altered + their new values and the condition
     * where if true, this change should be applied.
     */

    private static final String updateElementTable = "UPDATE " + elementTable
            + " SET [assignValues] WHERE [condition]";

    /**
     * SQL statement to update the stats table - replace [assignValues] and
     * [condition] with items to be altered + their new values and the condition
     * where if true, this change should be applied.
     */

    private static final String updateStatsTable = "UPDATE " + statsTable
            + " SET [assignValues] WHERE [condition]";

    // ----------------------------------------------------------------------------------

    /**
     * SQL statement to delete from the piece table - replace [condition] the
     * condition where if true, this deletion should be applied.
     */

    private static final String deleteFromPieceTable = "DELETE FROM "
            + pieceTable + " WHERE [condition]";

    /**
     * SQL statement to delete from the specific table - replace [condition] the
     * condition where if true, this deletion should be applied.
     */

    private static final String deleteFromSpecificTable = "DELETE FROM "
            + specificTable + " WHERE [condition]";

    /**
     * SQL statement to delete from the quote table - replace [condition] the
     * condition where if true, this deletion should be applied.
     */

    private static final String deleteFromQuoteTable = "DELETE FROM "
            + quoteTable + " WHERE [condition]";

    /**
     * SQL statement to delete from the element table - replace [condition] the
     * condition where if true, this deletion should be applied.
     */

    private static final String deleteFromElementTable = "DELETE FROM "
            + elementTable + " WHERE [condition]";

    /**
     * SQL statement to delete from the stats table - replace [condition] the
     * condition where if true, this deletion should be applied.
     */

    private static final String deleteFromStatsTable = "DELETE FROM "
            + statsTable + " WHERE [condition]";

    // ----------------------------------------------------------------------------------

    /**
     * SQL statement to insert into the piece table - replace [values] with the
     * values to be inserted.
     */

    private static final String insertIntoPieceTable = "INSERT INTO "
            + pieceTable + " VALUES ([values])";

    /**
     * SQL statement to insert into the specific table - replace [values] with
     * the values to be inserted.
     */

    private static final String insertIntoSpecificTable = "INSERT INTO "
            + specificTable + " VALUES ([values])";

    /**
     * SQL statement to insert into the quote table - replace [values] with the
     * values to be inserted.
     */

    private static final String insertIntoQuoteTable = "INSERT INTO "
            + quoteTable + " VALUES ([values])";

    /**
     * SQL statement to insert into the element table - replace [values] with
     * the values to be inserted.
     */

    private static final String insertIntoElementTable = "INSERT INTO "
            + elementTable + " VALUES ([values])";

    /**
     * SQL statement to insert into the quote table - replace [values] with the
     * values to be inserted.
     */

    private static final String insertIntoStatsTable = "INSERT INTO "
            + statsTable + " VALUES ([values])";

    // ----------------------------------------------------------------------------------

    /**
     * SQLiteDatabase representing the English database
     */

    private SQLiteDatabase dataBase;

    /**
     * Instance of DatabaseHelper class utilised to 'access' database
     */

    private DatabaseHelper baseHelper;

    /**
     * Content of the Database Manipulator (ManipulateDatabase class)
     */

    private Activity context;

    /**
     * Boolean to store whether or not this is the first time this base/version
     * has been launched
     */

    private boolean initialLaunch = false;

    /**
     * File for the BufferedReader to read into Database
     */

    private File pieceList;

    /**
     * Method to get pieceText file from assets for BufferedReader to use
     * 
     * @throws IOException
     */

    private InputStream getPieceTextFile(Context ctx) throws IOException {

        try {
            AssetManager manager = ctx.getAssets();
            InputStream is = manager.open("pieceText.txt");
            return is;
        } catch (Exception e) {
            System.out.println("Asset Manager/IS not work.");
            e.printStackTrace();
            return null;
        }
    }

    private BufferedReader pieceScan = new BufferedReader(
            new InputStreamReader(getPieceTextFile(this.context), "UTF-8"));

    /*
     * private File specificList = new File("specificList.txt");
     * 
     * private BufferedReader specificScan = new BufferedReader(new FileReader(
     * "specificList.txt"));
     * 
     * private File elementList = new File("elementList.txt");
     * 
     * private BufferedReader elementScan = new BufferedReader(new FileReader(
     * "elementList.txt"));
     * 
     * private File quoteList = new File("quoteList.txt");
     * 
     * private BufferedReader quoteScan = new BufferedReader(new FileReader(
     * "quoteList.txt"));
     */
    /**
     * Method to open the database
     */

    private void openSesame() {
        try {
            dataBase = baseHelper.getWritableDatabase();
        } catch (SQLiteException e) {
            System.out.println("Database couldn't be opened");
            e.printStackTrace();
        }
    }

    /**
     * Method to close the database
     */

    private void closeSesame() {
        baseHelper.close();
    }

    public ArrayList<String> getPieces(String typePiece) {
        try {
            openSesame();
            ArrayList<String> pieces = new ArrayList<String>();
            String command = searchPieceTable
                    .replace("[columnName]", typeField);
            command = command.replace("[value]", typePiece);

            Cursor cursor = dataBase.rawQuery(command, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                pieces.add(cursor.getString(cursor.getColumnIndex(pieceField)));
                cursor.moveToNext();
            }
            closeSesame();
            return pieces;
        } catch (SQLiteException e) {
            e.printStackTrace();
            System.out.println("SQL Problem");
            ArrayList<String> defaultReturn = new ArrayList<String>();
            defaultReturn.add("There is is default item.");
            defaultReturn.add("Because the proper ArrayList");
            defaultReturn.add("Wasn't filled properly");
            return defaultReturn;
        }
    }

    public ArrayList<String> getAllPieces() {

        try {
            openSesame();
            ArrayList<String> allPieces = new ArrayList<String>();
            String instruction = "SELECT * FROM pieceTable";

            Cursor cursor = dataBase.rawQuery(instruction, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                allPieces.add(cursor.getString(cursor
                        .getColumnIndex(pieceField)));
                cursor.moveToNext();
            }

            closeSesame();
            return allPieces;
        } catch (Exception e) {
            e.printStackTrace();
            ArrayList<String> defaultList = new ArrayList<String>();
            defaultList.add("Catch default");
            return defaultList;
        }

    }

    /**
     * Constructor
     * 
     * @param contextIn
     *            Context of the MainpuxlateDatabase class
     * @throws IOException
     */

    public ManipulateDatabase(Activity contextIn) throws IOException {
        baseHelper = new DatabaseHelper(contextIn);
        context = contextIn;
        openSesame();
        String line;
        String x;
        String pieceName;
        String isDrama;
        String point;
        String element;
        String quote;
        String isCharacter;
        int commaPoint;
        int commaPoint2;

        if (true) {
            while ((x = pieceScan.readLine()) != null) {
                line = pieceScan.readLine();
                System.out.println(line);
                commaPoint = line.indexOf(",");
                pieceName = line.substring(0, commaPoint);
                isDrama = line.substring(commaPoint + 1, line.length() - 1);
                ContentValues pv = new ContentValues();
                pv.put(pieceField, pieceName);
                pv.put(typeField, isDrama);
                dataBase.insert(pieceTable, null, pv);

                System.out.println("DAMN YOU MORONIC CRETIN!!");
            }

            /*
             * while ((x = specificScan.readLine()) != null) { line =
             * specificScan.readLine(); System.out.println(line); commaPoint =
             * line.indexOf(","); point = line.substring(0, commaPoint);
             * commaPoint2 = (line .substring(commaPoint + 1, line.length() - 1)
             * .indexOf(",")); pieceName = line.substring(commaPoint + 1,
             * commaPoint2); element = line.substring(commaPoint2 + 1,
             * line.length() - 1);
             * 
             * ContentValues sv = new ContentValues(); sv.put(pointField,
             * point); sv.put(pieceField, pieceName); sv.put(elementField,
             * element); dataBase.insert(specificTable, null, sv); } while ((x =
             * elementScan.readLine()) != null) { line = elementScan.readLine();
             * System.out.println(line); commaPoint = line.indexOf(","); element
             * = line.substring(0, commaPoint); commaPoint2 = (line
             * .substring(commaPoint + 1, line.length() - 1) .indexOf(","));
             * pieceName = line.substring(commaPoint + 1, commaPoint2);
             * isCharacter = line .substring(commaPoint2 + 1, line.length() -
             * 1);
             * 
             * ContentValues ev = new ContentValues(); ev.put(elementField,
             * element); ev.put(pieceField, pieceName); ev.put(elTypeField,
             * isCharacter); dataBase.insert(elementTable, null, ev);
             * 
             * } while ((x = quoteScan.readLine()) != null) { line =
             * quoteScan.readLine(); System.out.println(line); commaPoint =
             * line.indexOf(","); quote = line.substring(0, commaPoint); point =
             * line.substring(commaPoint + 1, line.length() - 1);
             * 
             * ContentValues qv = new ContentValues(); qv.put(quoteField,
             * quote); qv.put(pointField, point); dataBase.insert(quoteTable,
             * null, qv); }
             */

        }

    }

    // ----------------------------------------------------------------------------------

    /**
     * Database helper - Three basic methods - required database tasks
     * 
     * @author Daniel Lawson
     * @version 30/12/13
     */

    private class DatabaseHelper extends SQLiteOpenHelper {

        private DatabaseHelper(Context theContext) {
            super(theContext, databaseName, null, databaseVersion);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(createPieceTable);
                /*
                 * db.execSQL(createSpecificTable);
                 * db.execSQL(createQuoteTable); db.execSQL(createElementTable);
                 * db.execSQL(createStatsTable);
                 */
                initialLaunch = true;

            } catch (SQLiteException e) {
                System.out.println("Failed to create table(s)");
                e.printStackTrace();
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + pieceTable);
            db.execSQL("DROP TABLE IF EXISTS " + specificTable);
            db.execSQL("DROP TABLE IF EXISTS " + quoteTable);
            db.execSQL("DROP TABLE IF EXISTS " + elementTable);
            db.execSQL("DROP TABLE IF EXISTS " + statsTable);
            onCreate(db);

        }

    }
}

解决方案

private BufferedReader pieceScan = new BufferedReader(
        new InputStreamReader(getPieceTextFile(this.context), "UTF-8"));

This initializer code runs when your class is instantiated. this.context is null. This is shown in the logcat in

01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.getPieceTextFile(ManipulateDatabase.java:387)
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.<init>(ManipulateDatabase.java:398)

where <init> refers to constructor and member variable initialization. You initialize context in constructor, but constructor is only run after member variable initialization.

Just move the pieceScan initialization to the constructor.

这篇关于获取使用getAssets资产的文件()始终失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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