文件选择器Android Force关闭错误

时间:2012-10-08 14:51:21

标签: java android android-intent

我正在编写Android应用程序,我在其中将文件添加到我的共享文件夹中。

这是我打开对话框(用于文件浏览)的代码

  Intent pickFileIntent = new Intent();
  pickFileIntent.setAction(Intent.ACTION_GET_CONTENT);
  pickFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
  pickFileIntent.setType("*/*");
  Intent chooserIntent = Intent.createChooser(pickFileIntent,
  getText(R.string.choosefile_title));
  //Toast.makeText(FileShare.this, "",1).show();
  startActivityForResult(chooserIntent, PICK_FILE_REQUEST);

它适用于Image和其他mp3文件。 但当我点击文件浏览器时,它给了我错误。 以下是文件浏览器和logcat的代码

         public void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            /* If no data is supplied, get default from the FileProvider */
            if (getIntent().getData() == null) {
              getIntent().setData(FileProvider.CONTENT_URI);
            }
            String currentPath = FileProvider.getPath(getIntent().getData());

            /* Special case for "/". Wouldn't need this is we have a class to 
             * do path joining for us */
            if (currentPath.equals("/"))
              currentPath = "";
           mPaths.push(currentPath);

            /* Populate list with the root content */
        //Error in following line when i debug
            Cursor c = managedQuery(getIntent().getData(), null, null, null, null);
            changeCursor(c);
            registerForContextMenu(getListView());
          }

logcat的

        10-08 19:18:55.867: E/AndroidRuntime(16386): FATAL EXCEPTION: main
        10-08 19:18:55.867: E/AndroidRuntime(16386): java.lang.RuntimeException: Unable
        to start activity   
                  ComponentInfo{com.fyp.fileshare/com.fyp.fileshare.FileBrowser}:                                                 
        java.lang.ClassCastException: java.lang.Integer
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at             
        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.os.Handler.dispatchMessage(Handler.java:99)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.os.Looper.loop(Looper.java:130)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.app.ActivityThread.main(ActivityThread.java:3687)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at java.lang.reflect.Method.invokeNative(Native Method)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at             
        java.lang.reflect.Method.invoke(Method.java:507)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at dalvik.system.NativeStart.main(Native Method)
        10-08 19:18:55.867: E/AndroidRuntime(16386): Caused by: java.lang.ClassCastException: java.lang.Integer
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at com.fyp.fileshare.FileProvider$1.compare(FileProvider.java:103)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at com.fyp.fileshare.FileProvider$1.compare(FileProvider.java:1)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at java.util.TimSort.sort(TimSort.java:199)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at java.util.TimSort.sort(TimSort.java:169)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at java.util.Arrays.sort(Arrays.java:2090)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at java.util.Collections.sort(Collections.java:1965)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at com.fyp.fileshare.FileProvider.getCursorForFiles(FileProvider.java:101)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at com.fyp.fileshare.FileProvider.query(FileProvider.java:60)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.content.ContentProvider$Transport.query(ContentProvider.java:187)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.content.ContentResolver.query(ContentResolver.java:262)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.app.Activity.managedQuery(Activity.java:1550)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at com.fyp.fileshare.FileBrowser.onCreate(FileBrowser.java:68)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
        10-08 19:18:55.867: E/AndroidRuntime(16386):    ... 11 more

来自#100的行:

100// Sort listing by display name. 
101 Collections.sort(fileList, new Comparator<Object[]>() { 
102 public int compare(Object[] r1, Object[] r2) { 
103 return ((String) r1[1]).compareTo((String) r2[2]); } });

1 个答案:

答案 0 :(得分:0)

问题是return((String)r1 [1])。compareTo((String)r2 [2]);其中一个不是String

回答:njzk2