在活动外使用getAssets

5
我尝试在DatabaseHandler类中解析一个文件,但Eclipse报错:

方法getAssets()未定义于类型DatabaseHandler

这是代码:

 public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 15;

    public DatabaseHandler(Context context) {
        super(context, "rettinfo", null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("Create: ", "Creating antidotlist");
        String CREATE_ANTIDOT_TABLE = "CREATE TABLE antidots (id INTEGER PRIMARY KEY antidot TEXT, dos TEXT)";
        Log.d("Create: ", CREATE_ANTIDOT_TABLE);
        db.execSQL(CREATE_ANTIDOT_TABLE);

        InputStream antidots = getAssets().open("antidot/antidots");
        InputStreamReader input = new InputStreamReader(antidots);
        BufferedReader buffreader = new BufferedReader(input,2*1024);
        String line;
        while ((line = buffreader.readLine()) != null) {
            String[] point_t = line.split(",");
        }
        antidots.close();
    }

}

给Tim的更新

这是如何使eclipse不出现任何错误的方法。

int i = 0;
InputStream antidots;
    try {
        antidots = mCtx.getAssets().open("antidot/antidots");
        InputStreamReader input = new InputStreamReader(antidots);
        BufferedReader buffreader = new BufferedReader(input,2*1024);
        String line;
        while ((line = buffreader.readLine()) != null) {
            i++;
            ContentValues values = new ContentValues();
            String[] antidot = line.split("#");
            int id = Integer.parseInt(antidot[0]);
            values.put("id", id);
            values.put("antidot", antidot[1]);
            values.put("dos", antidot[2]);  
            db.insert("antidots", null, values);                     
        }
        antidots.close();           
    } catch (IOException e) {
        e.printStackTrace();
    }

是的,try catch 是很正常的。每当你处理文件 I/O 时,它都会让你将代码包装在 try/catch 中。这是因为文件 I/O 操作有很多常见的失败方式。如果没有 try/catch,你的程序在 I/O 出现问题时将会崩溃。 - FoamyGuy
还要注意的是,文件I/O的try catch是由Java约定引起的,而不是特定于Eclipse的。 - FoamyGuy
1个回答

19

将从构造函数获取的Context引用存储起来,然后在该引用上调用getAssets()方法。

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 15;
    private Context mCtx; //<-- declare a Context reference
    public DatabaseHandler(Context context) {
        super(context, "rettinfo", null, DATABASE_VERSION);
        mCtx = context; //<-- fill it with the Context you are passed
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("Create: ", "Creating antidotlist");
        String CREATE_ANTIDOT_TABLE = "CREATE TABLE antidots (id INTEGER PRIMARY KEY antidot TEXT, dos TEXT)";
        Log.d("Create: ", CREATE_ANTIDOT_TABLE);
        db.execSQL(CREATE_ANTIDOT_TABLE);

        InputStream antidots = mCtx.getAssets().open("antidot/antidots"); //<-- call getAssets on your Context object.
        InputStreamReader input = new InputStreamReader(antidots);
        BufferedReader buffreader = new BufferedReader(input,2*1024);
        String line;
        while ((line = buffreader.readLine()) != null) {
            String[] point_t = line.split(",");
        }
        antidots.close();
    }

}

谢谢您的帖子,Eclipse要求用try/catch包围是正常的吗? - Laire
它想要包围哪些行? - FoamyGuy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接