我開發Android一個發射器同時啓動應用程序它給了我這個錯誤Android-不幸的是應用程序已經停止
我的代碼
MainActivity
package com.Aaps.androidlauncher;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends Activity
{
DrawerAdapter drawerAdapterObject;
GridView drawerGrid;
IntentFilter filter;
class Pac
{
Drawable icon;
String name;
String label;
}
Pac[] pacs;
PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pm = getPackageManager();
set_pacs();
drawerGrid = (GridView) findViewById(R.id.content);
drawerAdapterObject = new DrawerAdapter(this,pacs);
drawerGrid.setAdapter(drawerAdapterObject);
drawerGrid.setOnItemClickListener(new DrawerClickListener(this,pacs,pm));
filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(new PacReciever(),filter);
}
public void set_pacs()
{
final Intent mainIntent = new Intent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pacList = pm.queryIntentActivities(mainIntent, 0);
pacs = new Pac[pacList.size()];
for(int i = 0; i < pacList.size();i++)
{
pacs[i] = new Pac();
pacs[i].icon=pacList.get(i).loadIcon(pm);
pacs[i].name = pacList.get(i).activityInfo.packageName;
pacs[i].label = pacList.get(i).loadLabel(pm).toString();
}
new SortApps().exchange_sort(pacs);
drawerAdapterObject = new DrawerAdapter(this,pacs);
drawerGrid.setAdapter(drawerAdapterObject);
drawerGrid.setOnItemClickListener(new DrawerClickListener(this,pacs,pm));
}
public class PacReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
set_pacs();
}
}
}
主要活動XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xm lns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.Aaps.androidlauncher.MainActivity" >
<SlidingDrawer
android:id="@+id/Drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle" >
<GridView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Grey_Transparent"
android:columnWidth="40dp"
android:gravity="center"
android:horizontalSpacing="30dp"
android:numColumns="auto_fit"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
android:stretchMode="columnWidth"
android:verticalSpacing="30dp" />
<ImageView
android:id="@+id/handle"
android:layout_width="80dp"
android:layout_height="30dp"
android:background="@drawable/handle" />
</SlidingDrawer>
</RelativeLayout>
@color/Grey_transparent
是從我製作的xml這個代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
`enter code here` <color name="Grey_Transparent">#80000000</color>
</resources>
DrawerAdapter類
package com.Aaps.androidlauncher;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class DrawerAdapter extends BaseAdapter{
Context mContext;
MainActivity.Pac[] pacsForAdapter;
public DrawerAdapter(Context c, MainActivity.Pac pacs[])
{
mContext = c;
pacsForAdapter = pacs;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pacsForAdapter.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
static class ViewHolder
{
TextView text;
ImageView icon;
}
@Override
public View getView(int pos, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
{
convertView = li.inflate(R.layout.drawer_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView)convertView.findViewById(R.id.icon_text);
viewHolder.icon = (ImageView)convertView.findViewById(R.id.icon_image);
convertView.setTag(viewHolder);
}
else
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.text.setText(pacsForAdapter[pos].label);
viewHolder.icon.setImageDrawable(pacsForAdapter[pos].icon);
return convertView;
}
}
的DrawerClickListener類
package com.Aaps.androidlauncher;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class DrawerClickListener implements OnItemClickListener{
Context mContext;
MainActivity.Pac[] pacsForAdapter;
PackageManager pmForListener;
public DrawerClickListener(Context c,MainActivity.Pac[] pacs , PackageManager pm)
{
mContext = c;
pacsForAdapter = pacs;
pmForListener = pm;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Intent launchIntent = pmForListener.getLaunchIntentForPackage(pacsForAdapter[pos].name);
mContext.startActivity(launchIntent);
}
}
logcat的
11-01 00:01:47.734: D/dalvikvm(1920): GC_FOR_ALLOC freed 45K, 4% free 2817K/2920K, paused 21ms, total 22ms
11-01 00:01:47.734: I/dalvikvm-heap(1920): Grow heap (frag case) to 3.409MB for 635808-byte allocation
11-01 00:01:47.794: D/dalvikvm(1920): GC_FOR_ALLOC freed <1K, 3% free 3438K/3544K, paused 53ms, total 53ms
11-01 00:01:48.254: D/dalvikvm(1920): GC_FOR_ALLOC freed 26K, 3% free 3892K/3992K, paused 24ms, total 24ms
11-01 00:01:48.334: D/AndroidRuntime(1920): Shutting down VM
11-01 00:01:48.334: W/dalvikvm(1920): threadid=1: thread exiting with uncaught exception (group=0xb2d0cb20)
11-01 00:01:48.344: E/AndroidRuntime(1920): FATAL EXCEPTION: main
11-01 00:01:48.344: E/AndroidRuntime(1920): Process: com.Aaps.androidlauncher, PID: 1920
11-01 00:01:48.344: E/AndroidRuntime(1920): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Aaps.androidlauncher/com.Aaps.androidlauncher.MainActivity}: java.lang.NullPointerException
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.os.Handler.dispatchMessage(Handler.java:102)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.os.Looper.loop(Looper.java:136)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-01 00:01:48.344: E/AndroidRuntime(1920): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 00:01:48.344: E/AndroidRuntime(1920): at java.lang.reflect.Method.invoke(Method.java:515)
11-01 00:01:48.344: E/AndroidRuntime(1920): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-01 00:01:48.344: E/AndroidRuntime(1920): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-01 00:01:48.344: E/AndroidRuntime(1920): at dalvik.system.NativeStart.main(Native Method)
11-01 00:01:48.344: E/AndroidRuntime(1920): Caused by: java.lang.NullPointerException
11-01 00:01:48.344: E/AndroidRuntime(1920): at com.Aaps.androidlauncher.MainActivity.set_pacs(MainActivity.java:66)
11-01 00:01:48.344: E/AndroidRuntime(1920): at com.Aaps.androidlauncher.MainActivity.onCreate(MainActivity.java:36)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.Activity.performCreate(Activity.java:5231)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-01 00:01:48.344: E/AndroidRuntime(1920): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-01 00:01:48.344: E/AndroidRuntime(1920): ... 11 more
,我是一個小白,請幫助
並要求別的。