Android 正在更正logcat错误“尝试写入空数组”

Android 正在更正logcat错误“尝试写入空数组”,android,Android,我正在编写一个非常基本的家庭启动器,我在AppsGridActivity中创建了一个类AppsInfoModel、一个CustomAdapter和一个GridView 下面是我在活动中填充适配器的代码 appsGrid = (GridView) findViewById(R.id.appsGrid); appsInfo = new AppsInfoModel(this); appsGrid.setAdapter(new CustomAdapter(this,appsInf

我正在编写一个非常基本的家庭启动器,我在AppsGridActivity中创建了一个类AppsInfoModel、一个CustomAdapter和一个GridView

下面是我在活动中填充适配器的代码

    appsGrid = (GridView) findViewById(R.id.appsGrid);
    appsInfo = new AppsInfoModel(this);
    appsGrid.setAdapter(new CustomAdapter(this,appsInfo));
这是适配器

public class CustomAdapter extends BaseAdapter {

Context context;
AppsInfoModel appInfo;
CustomAdapter(Context context,AppsInfoModel appInfo) {
    this.context = context;
    this.appInfo = appInfo;
}

@Override
public int getCount() {
    return appInfo.getSize();
}

@Override
public Object getItem(int position) {
    return appInfo.getItem(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.single_grid, parent, false);

        ImageView icon = (ImageView) convertView.findViewById(R.id.app_icon);
        icon.setImageDrawable(appInfo.getIcon(position));

        TextView label = (TextView) convertView.findViewById(R.id.app_label);
        label.setText(appInfo.getLabel(position));
        return convertView;
    }
    else
        return convertView;
}
}

模型班

public class AppsInfoModel {
String label[];
String name[];
Drawable icon[];
PackageManager pm;
List<ResolveInfo> appsInfo;

AppsInfoModel(Context context){
    pm=context.getPackageManager();
    appsInfo = pm.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER), 0);
    for(int i=0;i<appsInfo.size();i++){
        label[i]= (appsInfo.get(i).loadLabel(pm)).toString();//***This is where problem lies according to LogCat***
        name[i]= (appsInfo.get(i)).activityInfo.packageName;
        icon[i]=(appsInfo.get(i)).activityInfo.loadIcon(pm);
    }
}

Drawable getIcon(int i){
    return icon[i];
}

String getLabel(int i){
    return label[i];
}

String getPackageName(int i){
    return name[i];
}
int getSize(){
    return appsInfo.size();
}

public ResolveInfo getItem(int i) {
    return appsInfo.get(i);
}
}
这是Logcat错误

 java.lang.RuntimeException: Unable to start activity ComponentInfo{example.home/example.home.AppsGridActivity}: java.lang.NullPointerException: Attempt to write to null array
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to write to null array
        at example.home.AppsInfoModel.<init>(AppsInfoModel.java:25)
        at example.home.AppsGridActivity.onCreate(AppsGridActivity.java:21)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
为什么会这样?
逻辑或语法有什么问题。

根据您发布的代码,您没有初始化这些数组:

String label[];
String name[];
Drawable icon[];
你应该这样做:

AppsInfoModel(Context context){
    pm=context.getPackageManager();
    appsInfo = pm.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER), 0);

    label = new String[appsInfo.size()];
    name = new String[appsInfo.size()];
    icon = new Drawable[appsInfo.size()];
    for(int i=0;i<appsInfo.size();i++){
        label[i]= (appsInfo.get(i).loadLabel(pm)).toString();
        name[i]= (appsInfo.get(i)).activityInfo.packageName;
        icon[i]=(appsInfo.get(i)).activityInfo.loadIcon(pm);
    }
}

根据您发布的代码,您没有初始化这些数组:

String label[];
String name[];
Drawable icon[];
你应该这样做:

AppsInfoModel(Context context){
    pm=context.getPackageManager();
    appsInfo = pm.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER), 0);

    label = new String[appsInfo.size()];
    name = new String[appsInfo.size()];
    icon = new Drawable[appsInfo.size()];
    for(int i=0;i<appsInfo.size();i++){
        label[i]= (appsInfo.get(i).loadLabel(pm)).toString();
        name[i]= (appsInfo.get(i)).activityInfo.packageName;
        icon[i]=(appsInfo.get(i)).activityInfo.loadIcon(pm);
    }
}

AppsInfoModel.java中的第25行是什么。这是因为数组为空。AppsInfoModel.java中的第25行是什么。这是因为您的数组为空。Thanx,它完成了位。很高兴我能够帮助您:Thanx,它完成了位。很高兴我能够帮助您: