2012-04-07 140 views
0

我想顯示所有安裝的應用程序及其圖標。點擊列表項後,我正在向該應用程序顯示相關記錄。java.lang.classcastException android.content.pm.ResolveInfo

應用程序和圖標顯示正常,但是當我單擊列表項時我收到以下錯誤。 java.lang.classcastException android.content.pm.ResolveInfo

final PackageManager pm = this.getPackageManager(); 

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

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

    final ArrayAdapter<ResolveInfo> adapter = new ArrayAdapter<ResolveInfo>(
      this, R.layout.installedapp, list) { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      if (convertView == null) 
       convertView = LayoutInflater.from(parent.getContext()) 
         .inflate(R.layout.installedapp, parent, false); 

      final String text = list.get(position).activityInfo.applicationInfo 
        .loadLabel(pm).toString(); 
      ((TextView) convertView.findViewById(R.id.text)).setText(text); 

      final Drawable drawable = list.get(position).activityInfo.applicationInfo 
        .loadIcon(pm); 
      ((ImageView) convertView.findViewById(R.id.image)) 
        .setImageDrawable(drawable); 

      return convertView; 
     } 

    }; 
    setListAdapter(adapter); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

} 

protected void onListItemClick(ListView l, View v, int position, long id) { 
    String item = (String) getListAdapter().getItem(position);///////---->error 
    List<String> app_item = dh.getRecordItem(item); 
    // Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 
    Intent intent = new Intent(ApplicationList.this, AppContent.class); 
    Bundle bundle = new Bundle(); 

    if (app_item.get(0).equals("No records")) { 
     Log.d("Set", "No Activity happened"); 
     bundle.putString("list", app_item.toString()); 
    } else { 
     Log.d("Set", "Act happened"); 
     bundle.putString("list", app_item.toString()); 
    } 
    intent.putExtras(bundle); 
    startActivity(intent); 

} 

我試圖打印日誌項的值,我得到這個值resolverInfo{405437c0 com.android.camera gallaru pickerp=0 o=0 m=0x108000} 但我Wnt信號只得到相機。所以我的代碼點擊工作。 我應該怎樣去解決這個錯誤。 謝謝。

回答

1

其實你是鑄造ResolveInfo對象爲String對象,這就是爲什麼你會得到例外..

試試這個

protected void onListItemClick(ListView l, View v, int position, long id) { 
ResolveInfo info = (ResolveInfo) getListAdapter().getItem(position); 
String item = info.activityInfo.name; 
. 
. 
. 

讓我知道發生什麼事..

+0

現在,我得到來自清錯誤但我gettin null指針異常。 '列表 app_item = dh.getRecordItem(item);' 在這一行。日誌項目正在打印com.android.camera gallary但我只需要相機gallary。我不想要包名稱我只想要應用程序名稱 – thej 2012-04-07 06:55:53

+0

什麼是dh?我看不到dh的任何聲明行? – user370305 2012-04-07 07:01:22

+0

它的我的數據庫對象我傳遞應用程序名稱來獲取相關記錄。但項目返回包名稱。所以我希望應用程序名稱作爲參數傳遞給dh.getRecordItem(item)函數。那麼如何獲得唯一的應用程序名稱? – thej 2012-04-07 07:07:04

相關問題