在Android中无法提供结果

时间:2013-07-29 09:27:21

标签: android runtimeexception onactivityresult

我从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

正在加载数据,我在控制台中看到这是正确加载的。然后它开始将联系人保存到数据库,并在第一次联系后崩溃。

1 个答案:

答案 0 :(得分:1)

您有一个空指针异常。它在您提交的堆栈跟踪中可见。 updateContacts()尝试使用无效参数(Intent)初始化null。好像组件名称无效。尝试检查一下,你可以使用调试器。