2014-01-22 71 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

相关问题