2011-03-29 32 views
0

的名單,我有以下代碼來獲取安裝在手機上的應用程序的列表,但我收到以下錯誤:誤差獲取應用程序

Type mismatch: cannot convert from element type Object to ResolveInfo" for the list "for (ResolveInfo rInfo : list)" and also "List is a raw type. References to generic type List should be parameterized" for "List list = pm.queryIntentActivities...

public class safety extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     PackageManager pm = this.getPackageManager(); 

     Intent intent = new Intent(Intent.ACTION_MAIN, null); 
     intent.addCategory(Intent.CATEGORY_LAUNCHER); 

     List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED); 
     for (ResolveInfo rInfo : list) { 
      Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
     } 
    } 
} 

有誰知道如何解決這些錯誤?

回答

1

試試這個:

public class Safety extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     PackageManager pm = this.getPackageManager(); 

     Intent intent = new Intent(Intent.ACTION_MAIN, null); 
     intent.addCategory(Intent.CATEGORY_LAUNCHER); 

     ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED); 
     for (ResolveInfo rInfo : list) { 
      System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
     } 

    } 
} 
+0

當我嘗試這種方式我得到的是一個黑色的屏幕當過我運行它。爲什麼會發生這種情況的任何建議? – 2011-03-29 11:44:55

+0

這是因爲您尚未調用setContentView。您的活動沒有任何可顯示的內容。檢查你的adb logcat。它將打印所有的應用程序名稱。 – Vinoth 2011-03-29 11:56:38

+0

非常感謝...我不熟悉Eclipse和開發android應用程序,所以我沒有關於logcat,那是第一個聽說它的人:) – 2011-03-29 13:23:12