所有Apps活动都无法加载android

时间:2013-01-26 23:42:23

标签: java android listview

我正在创建一个Android应用程序抽屉,但是当我调用该活动时它失败了。似乎第39行有点不对劲。 它看起来像NullPointerException。 你能帮我吗?

Java代码:

package com.mysoftware.mysoftwareos.mobile;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class AllAppsActivity extends ListActivity {
LinearLayout appsLinearLayout;
ListView list;
Intent intent;
private ArrayList<ResolveInfo> mApplicationList;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.allapps_screen);
    //Import views
    appsLinearLayout = (LinearLayout)findViewById(R.id.appsLinearLayout);

    //Set wallpaper
    appsLinearLayout.setBackgroundResource(R.drawable.images);

    //Load all apps
    final PackageManager pm = this.getPackageManager();

    final ArrayList<ResolveInfo> list =
            (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, 
                    PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list)
    {

        Log.i("TAG", ": Installed Applications " + rInfo.activityInfo.
                applicationInfo.loadLabel(pm).toString());
    }

    final ArrayAdapter<ResolveInfo> adapter = 
        new ArrayAdapter<ResolveInfo>(this, android.R.layout.simple_list_item_1, list)
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                convertView = super.getView(position, convertView, parent);
                final String text = list.get(position).activityInfo.
                    applicationInfo.loadLabel(pm).toString();
                ((TextView)convertView).setText(text);
                return convertView;
            }
        };
    setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final Intent intent = new Intent(Intent.ACTION_MAIN);
    final ActivityInfo info = mApplicationList.get(position).activityInfo;
    intent.setClassName(info.packageName, info.name);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    startActivity(intent);
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/appsLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<TextView
    android:id="@+id/TextView03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Apps"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="35dp" />


<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/ImageView03"
        android:layout_width="match_parent"
        android:layout_height="13dp"
        android:scaleType="fitXY"
        android:src="@drawable/seperator" />
</LinearLayout>

</LinearLayout>

logcat的:

01-27 01:35:24.610: E/AndroidRuntime(2150): FATAL EXCEPTION: main
01-27 01:35:24.610: E/AndroidRuntime(2150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mysoftware.mysoftwareos.mobile/com.mysoftware.mysoftwareos.mobile.AllAppsActivity}: java.lang.NullPointerException
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at  android.app.ActivityThread.access$600(ActivityThread.java:127)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.os.Looper.loop(Looper.java:137)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.ActivityThread.main(ActivityThread.java:4499)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at dalvik.system.NativeStart.main(Native Method)
01-27 01:35:24.610: E/AndroidRuntime(2150): Caused by: java.lang.NullPointerException
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.ApplicationPackageManager.queryIntentActivities(ApplicationPackageManager.java:440)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at com.mysoftware.mysoftwareos.mobile.AllAppsActivity.onCreate(AllAppsActivity.java:39)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.Activity.performCreate(Activity.java:4637)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
01-27 01:35:24.610: E/AndroidRuntime(2150):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
01-27 01:35:24.610: E/AndroidRuntime(2150):     ... 11 more

1 个答案:

答案 0 :(得分:0)

从我所看到的,由此:

final ArrayList<ResolveInfo> list =
            (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, 
                    PackageManager.PERMISSION_GRANTED);

intent仍为null,您尚未创建Intent对象。

我不确定您要尝试匹配的Intent是什么,但在使用它之前仍需要使用new关键字对其进行实例化。

例如

intent = new Intent (Intent.ACTION_MAIN);
来自同一个调用的

,您正在影响全局变量list。要么考虑拿出

final ArrayList<ResolveInfo>

因此,您正在使用全局list 删除该全局参考。

接下来,我不认为PackageManager.PERMISSION_GRANTEDthis method的有效标志,因为这似乎是结果常量。

最后:mApplicationList也没有实例化,但是你尝试从中读取。现在这可能设置在其他地方,我们没有完整的代码。这只会影响对List项的单击。