通过json活动保存sharedpref中对象的arraylist在com.google.gson.internal.bind.reflectivetypeadapterfactory$1.write(reflective)崩溃

j2cgzkjk  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(432)

你好,我正在尝试使用json保存应用程序的数组列表。我创建了一个新的arraylist“blockedapps”,我想在其中存储我的应用列表。我在设备上显示已安装应用程序的活动运行正常,但当我单击项目时,该活动会崩溃,并出现以下错误

''' 2020-12-03 15:32:01.554 12152-12152/com.codecity.LockScreen E/AndroidRuntime:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) '''

我的目标是存储对象的数组列表,包括sharedpreference中的drawable、strings和integer,并显示在另一个活动中。请帮帮我。
应用程序获取所有已安装应用程序的活动:
“”公共类应用程序列表扩展了appcompatactivity实现了clickinterface{

RecyclerView appListView;
ArrayList<AppModel> appModelList = new ArrayList<>();
ArrayList<AppModel> blockedApps = new ArrayList<>();
AppAdapter adapter,blockedadapter;
ProgressDialog progressDialog;

public AppList(){

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_list);

    appListView = (RecyclerView) findViewById(R.id.list_view);
    adapter = new AppAdapter(appModelList,this, (ClickInterface) this);
    blockedadapter = new AppAdapter(blockedApps,this, (ClickInterface) this);
    appListView.setLayoutManager(new LinearLayoutManager(this));
    appListView.setAdapter(adapter);

    loadData();

    progressDialog = new ProgressDialog(this);
    progressDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            getInstalledApps();

        }
    });

}

@Override
protected void onResume() {
    super.onResume();
    progressDialog.setTitle("Fetching Apps");
    progressDialog.setMessage("Loading");
    progressDialog.show();

}

private void getInstalledApps() {

    List<PackageInfo> packageInfos = getPackageManager().getInstalledPackages(0);

    for(int i = 0;i < packageInfos.size();i++ )
    {

        String name = packageInfos.get(i).applicationInfo.loadLabel(getPackageManager()).toString();
        Drawable icon = packageInfos.get(i).applicationInfo.loadIcon(getPackageManager());
        String packname = packageInfos.get(i).packageName;
        appModelList.add(new AppModel(name,icon,0,packname));

    }

    adapter.notifyDataSetChanged();

}

@Override
public void OnItemClick(int i, String appname, Drawable appIcon, int status, String pkgname) {

    String appIconString = appIcon.toString();
    blockedApps.add(new AppModel(appname,appIcon,1,pkgname));
    blockedadapter.notifyDataSetChanged();
    saveData(blockedApps);
    loadData();

}

private void saveData(ArrayList<AppModel> blockedApps) {

    SharedPreferences appSharedPrefs = getSharedPreferences("List",MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(blockedApps);
    prefsEditor.putString("MyObject", json);
    prefsEditor.apply();

}

private void loadData() {
    SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("MyObject", null);
    Type type = new TypeToken<ArrayList<AppModel>>() {}.getType();
    blockedApps = gson.fromJson(json, type);
    if (blockedApps == null) {
        blockedApps = new ArrayList<>();
        Toast.makeText(this, "Nu array", Toast.LENGTH_SHORT).show();
    }
}

}
'''
适配器类“”公共类appadapter扩展了recyclerview.adapter<appadapter.design\u adapter\u backend>{

ArrayList<AppModel> appModels = new ArrayList<>();
ArrayList<AppModel> blockedApps = new ArrayList<>();
private ClickInterface clickInterface;
Context con;
AppAdapter adapter;

public AppAdapter(ArrayList<AppModel> appModels, Context con,ClickInterface clickInterface) {
    this.appModels = appModels;
    this.con = con;
    this.clickInterface = clickInterface;

}

@NonNull
@Override
public design_adapter_backend onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(con).inflate(R.layout.app_adapter,parent,false);
    design_adapter_backend design = new design_adapter_backend(view);
    return design;
}

@Override
public void onBindViewHolder(@NonNull design_adapter_backend holder, int position) {

    AppModel app = appModels.get(position);
    holder.appName.setText(app.getAppName());
    holder.appIcon.setImageDrawable(app.getAppIcon());
    String pkgname = holder.packageName;
    String appName = holder.appName.toString();
    Drawable appIcon = holder.appIcon.getDrawable();
    View mView = holder.mView;

    if(app.getStatus() == 0)
    {

        holder.appStatus.setImageResource(R.drawable.unlock);

    }

    else
    {

        holder.appStatus.setImageResource(R.drawable.lock);

    }

        mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            clickInterface.OnItemClick(position,appName,appIcon,1,pkgname);

        }
    });

}

@Override
public int getItemCount() {
    if(appModels.size() <= 257896 && appModels.size() >= 0) {
        return appModels.size();
    }

    else
        return 0;
}

public static class design_adapter_backend extends RecyclerView.ViewHolder{

    TextView appName;
    ImageView appIcon,appStatus;
    View mView;
    String packageName;

    public design_adapter_backend(@NonNull View itemView) {
        super(itemView);

        appName = itemView.findViewById(R.id.app_name);
        appIcon = itemView.findViewById(R.id.app_icon);
        appStatus = itemView.findViewById(R.id.app_status);
        mView = itemView;

    }
}

}
' ' '
带有getter和setter的对象类
'''
公共类appmodel{

String appName;
Drawable appIcon;
int Status;
String packageName;

public AppModel(String appName, Drawable appIcon, int status, String packageName) {
    this.appName = appName;
    this.appIcon = appIcon;
    this.Status = status;
    this.packageName = packageName;
}

public String getAppName() {
    return appName;
}

public void setAppName(String appName) {
    this.appName = appName;
}

public Drawable getAppIcon() {
    return appIcon;
}

public void setAppIcon(Drawable appIcon) {
    this.appIcon = appIcon;
}

public int getStatus() {
    return Status;
}

public void setStatus(int status) {
    Status = status;
}

public String getPackageName() {
    return packageName;
}

public void setPackageName(String packageName) {
    this.packageName = packageName;
}

} '''
应用程序崩溃时的logcat

2020-12-03 17:04:37.581 16643-16667/? E/Netd: getNetworkForDns: getNetId from enterpriseCtrl is netid 0
2020-12-03 17:04:37.592 16865-17097/? E/NetdEventListenerService: handleMessage: { when=0 what=10001 obj=com.android.server.connectivity.NetdEventListenerService$DnsResultParams@88028ee target=com.android.server.connectivity.NetdEventListenerService$DnsEventHandler }
2020-12-03 17:04:43.086 32295-32295/com.codecity.amonguslockscreen E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.codecity.amonguslockscreen, PID: 32295
    java.lang.StackOverflowError: stack size 8192KB
        at java.lang.Integer.<init>(Integer.java:884)
        at java.lang.Integer.valueOf(Integer.java:867)
        at java.lang.reflect.Field.get(Native Method)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:124)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
2020-12-03 17:04:43.089 32295-32295/com.codecity.amonguslockscreen E/AndroidRuntime:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题