onActivityResult崩溃“失败交付结果”

时间:2011-07-05 08:25:28

标签: android file android-activity crash

我有一个从AndDev网站获得的FileBrowser活动。问题是它停止工作并崩溃,这是logcat:

07-05 11:20:35.803: ERROR/AndroidRuntime(14706): FATAL EXCEPTION: main
07-05 11:20:35.803: ERROR/AndroidRuntime(14706): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {omar.quran1/omar.quran1.DataDownload}: java.lang.NullPointerException
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.os.Looper.loop(Looper.java:130)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at java.lang.reflect.Method.invoke(Method.java:507)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at dalvik.system.NativeStart.main(Native Method)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706): Caused by: java.lang.NullPointerException
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at omar.quran1.DataDownload.dirSize(DataDownload.java:387)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at omar.quran1.DataDownload.CheckIfFilesAlreadyExist(DataDownload.java:372)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at omar.quran1.DataDownload.onActivityResult(DataDownload.java:310)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
07-05 11:20:35.803: ERROR/AndroidRuntime(14706):     ... 11 more

一旦用户选择了他想要的文件夹,我就会这样做:

         StatFs stat = new StatFs(currentDirectory.getAbsolutePath());
                stat.restat(currentDirectory.getAbsolutePath());
                long available = ((long) stat.getAvailableBlocks() * (long) stat.getBlockSize());


                if((available / 1048576)>120)
                {
                    Intent resultIntent = new Intent();
                    resultIntent.putExtra("FilePath", currentDirectory.getPath());
                    setResult(Activity.RESULT_OK, resultIntent);
                    finish();
                }

关于来电者活动:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 
  switch(requestCode) { 
    case 1: { 
      if (resultCode == Activity.RESULT_OK) { 
          PATH = data.getStringExtra("FilePath"); 

          if(CheckIfFilesAlreadyExist(PATH)) //check if the files are already there.
          {
              if(!PATH.contains("Quran"))
                      PATH+="/Quran/";
              Main.Main_PATH=PATH;
              Toast.makeText(mContext, "The files have been found", Toast.LENGTH_LONG).show();

              Intent returnIntent = new Intent();
              setResult(RESULT_OK,returnIntent);        
              finish();

          }

          else{
          PATH+="/Quran/";
          Main.Main_PATH=PATH;
          edittext_Dialog.setText(PATH);
          goodFileChoosen=true;
          }
      }

      break; 
    } 
  } 
}

为什么FC会发生?

EDIT1:dirSize():

private long dirSize(File dir) {

    long result = 0;
    File[] fileList = dir.listFiles(); //line 387

    for(int i = 0; i < fileList.length; i++) {
        // Recursive call if it's a directory
        if(fileList[i].isDirectory()) {
            result += dirSize(fileList [i]);
        } else {
            // Sum the file size in bytes
            result += fileList[i].length();
        }
    }
    return result; // return the file size
}

1 个答案:

答案 0 :(得分:0)

FC正在您DataDownload.java方法的dirSize()行387处发生。在没有看到完整代码的情况下,我不知道为什么,但是查看该行上引用的对象并查找尚未创建/初始化的对象。

一种可能性是data.getStringExtra("FilePath");正在返回null