我通过此代码获取第三方应用程序列表。
ApplicationInfo ai;
final PackageManager pm = main.getPackageManager();
List<ApplicationInfo> packages1 = null;
int index = 0;
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : packages)
{
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
{
// IS A SYSTEM APP
}
else
{
packages1.add(index, appInfo);
index++;
}
}
但是当我评论这一行时,我得到了一个例外:
packages1.add(index, appInfo);
然后代码正确执行,但由于此行而导致异常。这是我的logcat:
07-01 05:19:32.058: W/dalvikvm(5558): threadid=8: thread exiting with uncaught exception (group=0x4001d7d0)
07-01 05:19:32.065: E/AndroidRuntime(5558): FATAL EXCEPTION: AsyncTask #1
07-01 05:19:32.065: E/AndroidRuntime(5558): java.lang.RuntimeException: An error occured while executing doInBackground()
07-01 05:19:32.065: E/AndroidRuntime(5558): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.lang.Thread.run(Thread.java:1096)
07-01 05:19:32.065: E/AndroidRuntime(5558): Caused by: java.lang.NullPointerException
07-01 05:19:32.065: E/AndroidRuntime(5558): at com.example.first.Fetch_Software.doInBackground(Fetch_Software.java:46)
07-01 05:19:32.065: E/AndroidRuntime(5558): at com.example.first.Fetch_Software.doInBackground(Fetch_Software.java:1)
07-01 05:19:32.065: E/AndroidRuntime(5558): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-01 05:19:32.065: E/AndroidRuntime(5558): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-01 05:19:32.065: E/AndroidRuntime(5558): ... 4 more
07-01 05:19:36.894: E/WindowManager(5558): Activity com.example.first.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@47faa8f8 that was originally added here
07-01 05:19:36.894: E/WindowManager(5558): android.view.WindowLeaked: Activity com.example.first.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@47faa8f8 that was originally added here
07-01 05:19:36.894: E/WindowManager(5558): at android.view.ViewRoot.<init>(ViewRoot.java:247)
07-01 05:19:36.894: E/WindowManager(5558): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171)
07-01 05:19:36.894: E/WindowManager(5558): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
07-01 05:19:36.894: E/WindowManager(5558): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-01 05:19:36.894: E/WindowManager(5558): at android.app.Dialog.show(Dialog.java:241)
07-01 05:19:36.894: E/WindowManager(5558): at com.example.first.MainActivity.onClick(MainActivity.java:114)
07-01 05:19:36.894: E/WindowManager(5558): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 05:19:36.894: E/WindowManager(5558): at java.lang.reflect.Method.invoke(Method.java:521)
07-01 05:19:36.894: E/WindowManager(5558): at android.view.View$1.onClick(View.java:2067)
07-01 05:19:36.894: E/WindowManager(5558): at android.view.View.performClick(View.java:2408)
07-01 05:19:36.894: E/WindowManager(5558): at android.view.View$PerformClick.run(View.java:8818)
07-01 05:19:36.894: E/WindowManager(5558): at android.os.Handler.handleCallback(Handler.java:587)
07-01 05:19:36.894: E/WindowManager(5558): at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 05:19:36.894: E/WindowManager(5558): at android.os.Looper.loop(Looper.java:123)
07-01 05:19:36.894: E/WindowManager(5558): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-01 05:19:36.894: E/WindowManager(5558): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 05:19:36.894: E/WindowManager(5558): at java.lang.reflect.Method.invoke(Method.java:521)
07-01 05:19:36.894: E/WindowManager(5558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
07-01 05:19:36.894: E/WindowManager(5558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
07-01 05:19:36.894: E/WindowManager(5558): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
我想你错过了申报行。
应该是:
List<ApplicationInfo> packages1 = null;
packages1 = new ArrayList<ApplicationInfo>();
答案 1 :(得分:3)
首先你这样做:
List<ApplicationInfo> packages1 = null;
然后你这样做:
packages1.add(index, appInfo);
转换为:
null.add(index, appInfo);
最终成为:
07-01 05:19:32.065: E/AndroidRuntime(5558): Caused by: java.lang.NullPointerException
07-01 05:19:32.065: E/AndroidRuntime(5558): at com.example.first.Fetch_Software.doInBackground(Fetch_Software.java:46)
在您的日志中。
答案 2 :(得分:3)
当您忘记初始化数据类型时发生空指针,这里您忘记初始化List。
只需按照以下方式初始化您的列表:
List<ApplicationInfo> packages1 = new List<ApplicationInfo>();
答案 3 :(得分:1)
package1尚未初始化,请尝试以下操作:
package1 = new ArrayList<ApplicationInfo>();