我正在尝试获取设备中安装的所有应用程序的列表。我为此目的编写了以下代码:
public static List<ApplicationInfo> getInstalledApplication(Context context) {
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> apps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));
return apps;
}
我正在使用Collections.sort
对我的应用进行排序。现在,在运行上面的代码时,我遇到的问题是:
无法在包0中输入0x7f050000(t = 4 e = 0)(错误-75)name = com.android.keyguard
Failure retrieving text 0x7f050000 in package com.android.keyguard
android.content.res.Resources$NotFoundException: String resource ID #0x7f050000
at android.content.res.Resources.getText(Resources.java:255)
at android.app.ApplicationPackageManager.getText(ApplicationPackageManager.java:1052)
at android.content.pm.PackageItemInfo.loadLabel(PackageItemInfo.java:120)
at com.example.khan.spycameralocker.SettingsFragment.getAllApp(SettingsFragment.java:53)
at com.example.khan.spycameralocker.SettingsFragment.onCreateView(SettingsFragment.java:35)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:16670)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16670)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
at android.view.View.measure(View.java:16670)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16670)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16670)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16670)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16670)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
at android.view.View.measure(View.java:16670)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2058)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1189)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1374)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1076)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5905)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:807)
at android.view.Choreographer.doCallbacks(Choreographer.java:601)
at android.view.Choreographer.doFrame(Choreographer.java:562)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop
我的应用不崩溃,但在发布时需要时间。请帮我解决这个问题。
编辑: 片段中的其他方法是:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
getAllApp();
lv = (ListView)view.findViewById(R.id.applist);
lv.setAdapter(new CustomAdapter(getActivity(),appNames,packageNames,appIcons));
return view;
}
private void getAllApp() {
final PackageManager packageManager = getActivity().getPackageManager();
// List<ApplicationInfo> installedApplications =
// packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> installedApplications =
getInstalledApplication(getActivity());
for (ApplicationInfo appInfo : installedApplications)
{
appNames.add(String.valueOf(appInfo.loadLabel(packageManager)));
packageNames.add(appInfo.packageName);
appIcons.add(getActivity().getPackageManager().getApplicationIcon(appInfo));
}
}