2014-01-22 109 views
0

我通常使用此代碼來填充我的listView,但是當我填充安裝在設備中的應用程序名稱時使用此代碼時,出現此代碼中的錯誤。你能幫我解釋一下嗎?IllegalStateException:無法執行活動的方法

AdminActivity.java

private void listviewItems() { 
final PackageManager pm = getPackageManager(); 
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); 
    ArrayList<HashMap<String, String>> arraylist_AppDetails = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> hashmap = null;   
    int id = 1; 

    for (ApplicationInfo packageInfo : packages) { 
     String appName = (String) pm.getApplicationLabel(packageInfo); 
     String packageName = packageInfo.packageName; 

     if(pm.getLaunchIntentForPackage(packageInfo.packageName)!= null && !pm.getLaunchIntentForPackage(packageInfo.packageName).equals("")){    hashmap = new HashMap<String, String>();     
      hashmap.put(Constants.APP_NAME, appName); 
      hashmap.put(Constants.PACKAGE_NAME, packageName); 
      hashmap.put(Constants.ID, String.valueOf(id++)); 
      arraylist_AppDetails.add(hashmap); 

      Log.i("arraylist_AppDetails", String.valueOf(arraylist_AppDetails)); 
     } 
    } 

    ListView lv_InstalledApp = (ListView) findViewById(R.id.lv_InstalledApp); 
    CustomAdapter_InstalledApps installAppsAdapter = new CustomAdapter_InstalledApps(
      AdminActivity.this, arraylist_AppDetails, 
      R.layout.attribute_applist, 
      Constants.APPDATA_ATTRIBUTE_KEYS, 
      Constants.APPDATA_ATTRIBUTES_VIEWS, true); 
    lv_InstalledApp.setAdapter(installAppsAdapter); 
} 

customAdapter.java

private Context context; 
private final ArrayList<HashMap<String, String>> mData; 
private ArrayList<HashMap<String, String>> unfilteredValues; 
private boolean chosenValues, ismarkAll = true; 
private int resource; 
private String[] from; 
private int[] to; 
private SimpleFilter mFilter; 
private ArrayList<String> arraylistAppId; 
private ArrayList<String> arraylistAppSelected; 

private final boolean[] mCheckedState; 

public CA_InstalledApps(Context context, 
     ArrayList<HashMap<String, String>> data, int resource, 
     String[] from, int[] to, boolean chosenValues) { 
    super(context, data, resource, from, to); 

    mCheckedState = new boolean[data.size()]; 

    this.context = context; 
    mData = data; 
    this.unfilteredValues = mData; 
    this.resource = resource; 
    this.from = from; 
    this.to = to; 
    this.arraylistAppId = new ArrayList<String>(); 
    this.arraylistAppSelected = new ArrayList<String>(); 
    this.chosenValues = chosenValues; 

} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(context); 
    final int pos = position; 
    // View rowView = null; 
    try { 
     // if(convertView == null){ 
     convertView = inflater.inflate(resource, null, true); 
     TextView tv_C_ID = (TextView) convertView.findViewById(to[0]); 
     TextView tv_C_APPNAME = (TextView) convertView.findViewById(to[1]); 
     TextView tv_C_PACKAGENAME = (TextView) convertView.findViewById(to[2]); 

     String C_ID = from[0]; 
     String C_APPNAME = from[1]; 
     String C_PACKAGENAME = from[2]; 

     // getting the value in hashmap 
     final String _id = unfilteredValues.get(position).get(C_ID) 
       .toString(); 
     String _appname = unfilteredValues.get(position).get(C_APPNAME) 
       .toString(); 
     String _packagename = unfilteredValues.get(position).get(C_PACKAGENAME) 
       .toString(); 


     tv_C_ID.setText(_id); 
     tv_C_APPNAME.setText(_appname); 
     tv_C_PACKAGENAME.setText(_packagename); 

     // Setting the ID 
     tv_C_ID.setId(position); 
     tv_C_APPNAME.setId(position); 
     tv_C_PACKAGENAME.setId(position); 

     // } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } catch (OutOfMemoryError E) { 
     E.printStackTrace(); 
    } 

    return convertView; 
} 

    // Other codes from here.... 

logcat的

01-23 09:39:01.964: E/AndroidRuntime(6214): FATAL EXCEPTION: main 
01-23 09:39:01.964: E/AndroidRuntime(6214): java.lang.IllegalStateException: Could not execute method of the activity 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.view.View$1.onClick(View.java:3698) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.view.View.performClick(View.java:4222) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.view.View$PerformClick.run(View.java:17273) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.os.Handler.handleCallback(Handler.java:615) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.os.Looper.loop(Looper.java:137) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.app.ActivityThread.main(ActivityThread.java:4895) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at dalvik.system.NativeStart.main(Native Method) 
01-23 09:39:01.964: E/AndroidRuntime(6214): Caused by: java.lang.reflect.InvocationTargetException 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at android.view.View$1.onClick(View.java:3693) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  ... 11 more 
01-23 09:39:01.964: E/AndroidRuntime(6214): Caused by: java.lang.NullPointerException 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at com.example.kioskmode.AdminActivity.listviewItems(AdminActivity.java:142) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at com.example.kioskmode.AdminActivity.viewInstalledApps(AdminActivity.java:100) 
01-23 09:39:01.964: E/AndroidRuntime(6214):  at com.example.kioskmode.AdminActivity.onButtonClicked(AdminActivity.java:53) 

回答

0

IllegalStateException是最外面的一個,但是您從Caused by:子句中看到S_2nd_AdminActivity.listviewItems()S_2nd_AdminActivity.java文件的第142行上發生根異常NullPointerException。 然後這個NPE升級,Android框架類最終拋出IllegalStateException

至於是什麼導致了NPE,是線142這一個:

hashmap.put(Constants.APP_NAME, appName); 

?您似乎永遠不會影響任何有效的Map實例到hashmap變量,在執行上述指令時,您粘貼的代碼仍然是nullnull不能被解除引用,虛擬機會拋出NullPointerException

+0

請參閱上面的修改。 – androidBoomer

+0

編輯我的答案 - 如果它看起來不正確,你仍然無法弄清楚,請提供行號(或哪一個是l.1​​42)。 – desseim

+0

oooh,我明白了。現在,我的問題是如何獲取每個appName和packageName,以便我可以訪問它.. – androidBoomer

相關問題