2012-06-26 9 views
5

que trato de analizar un archivo en mi clase DatabaseHandler pero Eclipse dicen:Uso getAssets fuera una actividad

Los getAssets método() está definido para el DatabaseHandler tipo

Este es el código:

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

} 

actualización para Tim

Esta es la forma en Eclipse no hacen ningún error

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í, el try catch es normal. Cada vez que trabaje con un archivo io hará que ajuste el código en try/catch. Hace esto porque hay muchas formas comunes en que las operaciones de E/S de archivos pueden fallar. Y sin el try/catch, su programa simplemente se bloqueará en caso de que haya un problema con el i/o – FoamyGuy

+0

También tenga en cuenta que el archivo i/o try catch se debe a una convención java, y no es específico de eclipse. – FoamyGuy

Respuesta

19

almacenar una referencia a la Context que se obtiene de la constructora a continuación, llamar getAssets() en esa referencia.

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

Gracias por su publicación, ¿es normal que Eclipse quiera rodearlo con try/catch? – Laire

+0

Quiere rodear qué líneas? – FoamyGuy

+0

Edito mi primera publicación – Laire

Cuestiones relacionadas