2012-06-26 6 views
5

cerco di analizzare un file nella mia classe DatabaseHandler ma Eclipse dicono:Uso getAssets fuori un'attività

I getAssets() viene definito per il tipo di DatabaseHandler

Questo è il codice:

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

} 

Aggiornamento per Tim

Questo è il modo in eclisse Non fare errori

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

sì il tentativo di cattura è normale. Ogni volta che hai a che fare con il file io, ti farà incastrare il codice in try/catch. Lo fa perché ci sono molti modi in cui le operazioni di i/o file possono fallire. E senza il try/catch il tuo programma si bloccherà semplicemente nel caso in cui ci sia un problema con l'i/o – FoamyGuy

+0

Nota anche che il file i/o try catch è dovuto ad una convenzione java, e non è specifico di eclipse. – FoamyGuy

risposta

19

memorizzare un riferimento alla Context che si ottiene dal costruttore quindi chiamare getAssets() su quella di riferimento.

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

Grazie per il tuo post, è normale che Eclipse voglia circondarsi di try/catch? – Laire

+0

Vuole circondare quali linee? – FoamyGuy

+0

Modifica il mio primo post – Laire