我正在创建一个 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>
日志猫:
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