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)
請參閱上面的修改。 – androidBoomer
編輯我的答案 - 如果它看起來不正確,你仍然無法弄清楚,請提供行號(或哪一個是l.142)。 – desseim
oooh,我明白了。現在,我的問題是如何獲取每個appName和packageName,以便我可以訪問它.. – androidBoomer