2012-06-26 12 views
5

staram się analizować plik w mojej klasie DatabaseHandler ale Eclipse powiedzieć:Korzystanie getAssets poza działalnością

getAssets() metoda jest zdefiniowana dla typu DatabaseHandler

Jest to kod:

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

} 

Aktualizacja Tim

To jak zaćmienie nie dokonywać żadnych błędów

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

tak próba złapania jest normalna. Za każdym razem, gdy masz do czynienia z plikiem io, spowoduje to, że owiniesz kod w try/catch. Robi to, ponieważ istnieje wiele typowych sposobów, w których operacje na plikach I/O mogą zawieść. I bez próby/catch twój program po prostu ulegnie awarii w przypadku, gdy wystąpi problem z i/o – FoamyGuy

+0

Należy również zauważyć, że plik i/o try catch jest wynikiem konwencji java i nie jest specyficzny dla środowiska Eclipse. – FoamyGuy

Odpowiedz

19

przechowywać odniesienie do Context które można uzyskać z konstruktora następnie wywołać getAssets() na ten odnośnik.

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

Dziękujemy za Twój wpis, czy to normalne, że Eclipse chce otaczać się try/catch? – Laire

+0

Chce otoczyć które linie? – FoamyGuy

+0

Edytuję swój pierwszy wpis – Laire

Powiązane problemy