gpt4 book ai didi

android - 纠正 logcat 错误 "Attempt to write to null array"

转载 作者:行者123 更新时间:2023-11-29 14:31:04 26 4
gpt4 key购买 nike

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

这是我在 Activity 中填充适配器的代码

    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);
}
}

关于android - 纠正 logcat 错误 "Attempt to write to null array",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com