我正在从 sdcard .vcf 数据加载,然后将其放入数据库。我用这段代码做到这一点:
public void loadContactsFromSdcard() {
Intent intent = new Intent(getBaseContext(), FileDialog.class);
intent.putExtra(FileDialog.START_PATH, Environment.getExternalStorageDirectory().getPath());
//can user select directories or not
intent.putExtra(FileDialog.CAN_SELECT_DIR, false);
//filter files, looking only for .vcf format
intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "vcf" });
startActivityForResult(intent, REQUEST_SAVE);
}
public synchronized void onActivityResult(final int requestCode,
int resultCode, final Intent data) {
String filePath = data.getStringExtra(FileDialog.RESULT_PATH);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_SAVE) {
uti.showToast(getBaseContext(), filePath);
File file = new File(filePath);
FileInputStream fis = null;
StringBuilder builder = new StringBuilder();
try {
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader((InputStream)fis, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println(builder.toString());
contactService.updateContacts(builder.toString());
} else if (requestCode == REQUEST_LOAD) {
System.out.println("Loading...");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Logger.getLogger(ContactListActivity.class.getName()).log(
Level.WARNING, "file not selected");
}
}
我认为这行得通,但是现在,显然行不通。我换了手机软件可能是这个问题。日志输出:
07-29 11:15:23.913: E/AndroidRuntime(16473): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { cmp=pl..stxcontactsync/.util.FileDialog (has extras) }} to activity {pl..stxcontactsync/pl..stxcontactsync.ContactListActivity}: java.lang.NullPointerException
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.app.ActivityThread.deliverResults(ActivityThread.java:2553)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.app.ActivityThread.access$2000(ActivityThread.java:121)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.os.Looper.loop(Looper.java:123)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.app.ActivityThread.main(ActivityThread.java:3701)
07-29 11:15:23.913: E/AndroidRuntime(16473): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 11:15:23.913: E/AndroidRuntime(16473): at java.lang.reflect.Method.invoke(Method.java:507)
07-29 11:15:23.913: E/AndroidRuntime(16473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
07-29 11:15:23.913: E/AndroidRuntime(16473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
07-29 11:15:23.913: E/AndroidRuntime(16473): at dalvik.system.NativeStart.main(Native Method)
07-29 11:15:23.913: E/AndroidRuntime(16473): Caused by: java.lang.NullPointerException
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.content.ComponentName.<init>(ComponentName.java:75)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.content.Intent.<init>(Intent.java:2705)
07-29 11:15:23.913: E/AndroidRuntime(16473): at pl..stxcontactsync.ContactServiceActivity.updateContacts(ContactServiceActivity.java:79)
07-29 11:15:23.913: E/AndroidRuntime(16473): at pl..stxcontactsync.ContactListActivity.onActivityResult(ContactListActivity.java:298)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
07-29 11:15:23.913: E/AndroidRuntime(16473): at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
07-29 11:15:23.913: E/AndroidRuntime(16473): ... 11 more
加载数据没问题,我在控制台中看到它已正确加载。然后它开始将联系人保存到数据库,并在第一次联系后崩溃。