将List <applicationinfo>转换为String []

时间:2015-05-13 23:40:28

标签: android string arraylist

我需要将List转换为String []。怎么做?

我试着用它:

 PackageManager packageManager = getPackageManager();
    List<ApplicationInfo> liste_aller_Anwendungen = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

    String[] strings = liste_aller_Anwendungen.toArray(new String[liste_aller_Anwendungen.size()]);

但这会产生以下logcat错误:

java.lang.ArrayStoreException: source[0] of type android.content.pm.ApplicationInfo cannot be stored in destination array of type java.lang.String[]
        at java.lang.System.arraycopy(Native Method)
        at java.util.ArrayList.toArray(ArrayList.java:523)
        at de.gestureanywhere.HintergrundService.onGesturePerformed(HintergrundService.java:152)
        at android.gesture.GestureOverlayView.fireOnGesturePerformed(GestureOverlayView.java:729)
        at android.gesture.GestureOverlayView.access$400(GestureOverlayView.java:55)
        at android.gesture.GestureOverlayView$FadeOutRunnable.run(GestureOverlayView.java:744)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5034)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
        at dalvik.system.NativeStart.main(Native Method)

由于

1 个答案:

答案 0 :(得分:1)

首先:

<T> T[]     toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

请注意,您尝试将ApplicationInfo列表存储在String列表中,这是不可能的。这样做的方式是这样的:

String[] strings =  new String[liste_aller_Anwendungen.size()];
for(int i = 0; i < liste_aller_Anwendungen.size(); i++) {
    strings[i] = liste_aller_Anwendungen[i].toString(); // or whatever you want, it just needs to be a `String`
}
相关问题