好的我正在使用SQLiteHelper类从URL资源加载CSV文件,然后加载到我的SQLite DB中。但是我收到一个不会停止应用程序的LogCat错误,但也没有将数据加载到我的应用程序中。
这是我第一次使用SQLite,所以可以提供任何建议我哪里出错?我提供了一个LogCat打印输出供参考。
MYSQliteHelper.java
public class MySQLiteHelper extends SQLiteOpenHelper {
String filename;
FileReader file;
//Database Version
private static final int DATABASE_VERSION = 1;
//Database Name
private static final String DATABASE_NAME = "CatalogueDB";
//Catalogue records table name
private static final String TABLE_RECORDS = "records";
//Catalogue records column names
private static final String KEY_ID = "id";
private static final String KEY_GROUP = "category";
private static final String KEY_NAME = "name";
private static final String KEY_IMAGE = "image";
private static final String KEY_DESC = "description";
private static final String KEY_ADDR = "address";
private static final String KEY_TELEPHONE = "telephone";
private static final String KEY_URL = "url";
private static final String KEY_LAT = "latitude";
private static final String KEY_LNG = "longitude";
//Array of Strings used to create database columns
private static final String[] COLUMNS = {KEY_ID, KEY_GROUP, KEY_NAME, KEY_IMAGE, KEY_DESC, KEY_ADDR,
KEY_TELEPHONE, KEY_URL, KEY_LAT, KEY_LNG};
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//SQL statement to create records (map markers) table
String CREATE_RECORDS_TABLE ="CREATE TABLE records (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"category TEXT, " +
"name TEXT, " +
"image TEXT, " +
"description TEXT, " +
"address TEXT, " +
"telephone TEXT, " +
"url TEXT, " +
"latitude TEXT, " +
"longitude TEXT" + ")";
//Create records (map markers) table
db.execSQL(CREATE_RECORDS_TABLE);
new CSVFileDownloader().execute(); // RETURN CSV FILE FROM URL/SERVER
//LOAD CSV FILE
try {
file = new FileReader(filename);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
System.out.println("FILE: " + file);
System.out.println("FILE DOWNLOADED: " + filename);
BufferedReader buffer = new BufferedReader(file);
String line = "";
//String tableName = TABLE_RECORDS;
//String[] columns = COLUMNS;
String str1 = "INSERT INTO " + TABLE_RECORDS + " (" + COLUMNS + ") values (";
String str2 = ");";
db.beginTransaction();
try {
while ((line= buffer.readLine()) != null) {
StringBuilder sb = new StringBuilder(str1);
String[] str = line.split(","); // CSV split
sb.append("'" + str[0] + "',");
sb.append(str[1] + "', ");
sb.append(str[2] + "', ");
sb.append(str[3] + "', ");
sb.append(str[4] + "', ");
sb.append(str[5] + "', ");
sb.append(str[6] + "', ");
sb.append(str[7] + "', ");
sb.append(str[8] + "', ");
sb.append(str[9] + "', ");
sb.append(str2);
db.execSQL(sb.toString()); //ADD CSV RECORD INTO DATABASE
}
db.setTransactionSuccessful();
db.endTransaction();
} catch (IOException e) {
e.printStackTrace();
}
}
然后我的AsynchTask
方法从网络资源获取我的CSV文件:
private class CSVFileDownloader extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials("username", "password"));
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.cardigan.cc/app/locations.csv");
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
filename = response.toString(); //LOAD RESPONSE(the file) INTO FILENAME
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String result = "";
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
N.B。私人课程在MySQLiteHelper.java
最后是LogCat打印输出。
02-22 18:21:31.360: W/dalvikvm(18407): threadid=5: spin on suspend #1 threadid=1 (pcf=0)
02-22 18:21:31.610: W/dalvikvm(18407): threadid=5: spin on suspend resolved in 1255 msec
02-22 18:21:33.490: W/ActivityThread(18407): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
02-22 18:21:34.290: W/System.err(18407): java.lang.NullPointerException
02-22 18:21:34.410: W/System.err(18407): at java.io.File.fixSlashes(File.java:185)
02-22 18:21:34.410: W/System.err(18407): at java.io.File.<init>(File.java:134)
02-22 18:21:34.410: W/System.err(18407): at java.io.FileInputStream.<init>(FileInputStream.java:105)
02-22 18:21:34.410: W/System.err(18407): at java.io.FileReader.<init>(FileReader.java:66)
02-22 18:21:34.410: W/System.err(18407): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper.onCreate(MySQLiteHelper.java:85)
02-22 18:21:34.410: W/System.err(18407): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-22 18:21:34.410: W/System.err(18407): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
02-22 18:21:34.410: W/System.err(18407): at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper.getAllMapMarkers(MySQLiteHelper.java:202)
02-22 18:21:34.410: W/System.err(18407): at uk.ac.aber.dwd.CeredigionTourism.MapActivity.onCreate(MapActivity.java:40)
02-22 18:21:34.410: W/System.err(18407): at android.app.Activity.performCreate(Activity.java:5240)
02-22 18:21:34.410: W/System.err(18407): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-22 18:21:34.410: W/System.err(18407): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-22 18:21:34.410: W/System.err(18407): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-22 18:21:34.410: W/System.err(18407): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-22 18:21:34.410: W/System.err(18407): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-22 18:21:34.410: W/System.err(18407): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 18:21:34.410: W/System.err(18407): at android.os.Looper.loop(Looper.java:137)
02-22 18:21:34.410: W/System.err(18407): at android.app.ActivityThread.main(ActivityThread.java:5048)
02-22 18:21:34.410: W/System.err(18407): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 18:21:34.410: W/System.err(18407): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 18:21:34.410: W/System.err(18407): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
02-22 18:21:34.410: W/System.err(18407): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
02-22 18:21:34.410: W/System.err(18407): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你得到一个NullPointerException,因为你试图用空文件名打开一个文件:
file = new FileReader(filename);
您似乎正在尝试异步初始化文件名,所以我猜测发生的事情是您在转到new FileReader(filename)
filename = response.toString();
代码
正确的解决方案是仅在AsyncTask
函数中启动onCreate()
,并在AsyncTask
完成后执行其余操作(即在其onPostExecute()
函数中)