2012-06-26 22 views
5

tôi cố gắng phân tích một tập tin trong lớp DatabaseHandler tôi nhưng Eclipse nói:Sử dụng getAssets bên ngoài một hoạt động

Các getAssets() phương pháp là undefined cho các loại DatabaseHandler

Đây là mã:

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(); 
    } 

} 

cập nhật cho Tim

Đây là cách nhật thực không thực hiện bất kỳ lỗi

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(); 
    } 
+0

có thử bắt là bình thường. Bất cứ lúc nào bạn đang xử lý tập tin io nó sẽ làm cho bạn quấn mã trong try/catch. Nó làm điều này bởi vì có rất nhiều cách phổ biến mà các hoạt động tập tin i/o có thể thất bại. Và nếu không có thử/bắt chương trình của bạn sẽ chỉ đơn giản là sụp đổ trong trường hợp có một vấn đề với i/o – FoamyGuy

+0

Cũng lưu ý rằng tập tin i/o thử bắt là do một quy ước java, và không phải là cụ thể để nhật thực. – FoamyGuy

Trả lời

19

Lưu trữ một tham chiếu đến Context mà bạn nhận được từ các nhà xây dựng sau đó gọi getAssets() trên tham chiếu đó.

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(); 
    } 

} 
+0

Cảm ơn bạn đã đăng bài của bạn, nó là bình thường mà Eclipse muốn bao quanh với try/catch? – Laire

+0

Nó muốn bao quanh những dòng nào? – FoamyGuy

+0

Tôi chỉnh sửa bài đăng đầu tiên – Laire

Các vấn đề liên quan