2013-07-29 32 views
0

我从sdcard.vcf数据加载,然后将其放入数据库。我用这个代码来做这件事:在Android中传递失败的结果

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

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

+0

问题是这样的: Intent intent = new Intent(getBaseContext(),ContactListActivity.class); \t \t startActivity(intent); 我更改为在构造函数中设置的“上下文”,但它仍然不起作用。我想我知道这个问题.. –

相关问题