gpt4 book ai didi

android - 没有连接适配器;单击后退按钮并再次启动应用程序时跳过布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:56 25 4
gpt4 key购买 nike

Android Studio 3.2 Canary 18
kotlin_version = 1.2.50

我有一个使用 recyclerview 和适配器的简单应用程序。当应用程序启动时加载所有数据。但是,当我单击后退按钮并再次启动该应用程序时。它不会显示数据(空白)。如果我从内存中清除应用程序并启动应用程序。数据将正常加载。

我正在从 sqlite 加载数据,并且每次都加载数据。因为它填充了 insectDataModelList

进入RecyclerView.java 源代码后,原因是mAdapter 为空。但是,我有当我将它设置为 recyclerview 时检查适配器是否正确。

void dispatchLayout() {
if (mAdapter == null) {
Log.e(TAG, "No adapter attached; skipping layout");
// leave the state in START
return;
}
...
}

我的MainActivity.java是Java的

public class MainActivity extends AppCompatActivity {
private RecyclerView rvInsects;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rvInsects = (RecyclerView)findViewById(R.id.recycler_view);

DatabaseManager databaseManager = DatabaseManager.getInstance(this);
databaseManager.queryAllInsects("friendlyName");
}

private void setupAdapter(List<InsectDataModel> insectDataModelList) {
final LayoutManager layoutManager = new LinearLayoutManager(
this, LinearLayoutManager.VERTICAL, false);
rvInsects.setLayoutManager(layoutManager);
rvInsects.setHasFixedSize(true);
final InsectAdapter insectAdapter = new InsectAdapter(insectDataModelList);
rvInsects.setAdapter(insectAdapter);
insectAdapter.notifyDataSetChanged();
}

/* Callback from database */
public void loadAllInsects(final Cursor cursor) {
InsectInteractorMapper insectInteractorMapper = new InsectInteractorMapperImp();
final List<InsectDataModel> insectDataModelList = insectInteractorMapper.map(cursor);
/* data loaded with 24 items */
setupAdapter(insectDataModelList);
}
}

InsectAdapter.kt 是 Kotlin。

class InsectAdapter(private val insectList: MutableList<InsectDataModel>)
: RecyclerView.Adapter<InsectAdapter.CustomInsectHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomInsectHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.insect_row_item, parent, false)
return CustomInsectHolder(view)
}

override fun onBindViewHolder(holder: CustomInsectHolder, position: Int) {
holder.tvFriendlyName.text = insectList[position].friendlyName
holder.tvScientificName.text = insectList[position].scientificName
}

override fun getItemCount(): Int {
return insectList.size
}

class CustomInsectHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val ivDangerLevel: DangerLevelView = itemView.findViewById(R.id.ivDangerLevel)
val tvFriendlyName: TextView = itemView.findViewById(R.id.tvFriendlyName)
val tvScientificName: TextView = itemView.findViewById(R.id.tvScientificName)
}
}

我使用rxjava2做查询的数据库

public class DatabaseManager {
private static DatabaseManager sInstance;
private MainActivity mainActivity;
private BugsDbHelper mBugsDbHelper;

public static synchronized DatabaseManager getInstance(MainActivity context) {
if (sInstance == null) {
sInstance = new DatabaseManager(context);
}

return sInstance;
}

private DatabaseManager(MainActivity context) {
mBugsDbHelper = new BugsDbHelper(context);

mainActivity = context;
}

@SuppressLint("CheckResult")
public void queryAllInsects(String sortOrder) {
final InsectStorageInteractorImp insectStorageInteractorImp
= new InsectStorageInteractorImp(new InsectStorageImp(mBugsDbHelper.getReadableDatabase()));

insectStorageInteractorImp.getAllSortedInsects(sortOrder)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Cursor>() {
Disposable disposable;

@Override
public void onSubscribe(Disposable d) {
disposable = d;
}

@Override
public void onSuccess(Cursor cursor) {
mainActivity.loadAllInsects(cursor);
disposable.dispose();
}

@Override
public void onError(Throwable e) {
disposable.dispose();
}
});
}
}

首次安装应用程序时,一切都按预期工作。如果你把它从内存中清除。但是,只有当您单击后退按钮,然后尝试启动应用程序时,它才会加载任何数据因为 mAdapter 在 RecyclerView 类中为 null。

当我点击后退按钮然后再次启动应用程序时。我得到的只是一个空白屏幕,即

enter image description here

更新的 DatabaseManager 类删除了单例并使用弱引用来确保 MainActivity 实例被垃圾回收。

public class DatabaseManager {
private WeakReference<MainActivity> mainActivity;
private BugsDbHelper mBugsDbHelper;

public DatabaseManager(MainActivity context) {
mBugsDbHelper = new BugsDbHelper(context);
mainActivity = new WeakReference<>(context);
}

@SuppressLint("CheckResult")
public void queryAllInsects(String sortOrder) {
final InsectStorageInteractorImp insectStorageInteractorImp
= new InsectStorageInteractorImp(new InsectStorageImp(mBugsDbHelper.getReadableDatabase()));

insectStorageInteractorImp.getAllSortedInsects(sortOrder)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Cursor>() {
Disposable disposable;

@Override
public void onSubscribe(Disposable d) {
disposable = d;
}

@Override
public void onSuccess(Cursor cursor) {
mainActivity.loadAllInsects(cursor);
disposable.dispose();
}

@Override
public void onError(Throwable e) {
disposable.dispose();
}
});
}
}

非常感谢您的任何建议,

最佳答案

当您单击后退按钮并重新启动应用程序时,将启动一个新的 MainActivity 实例。

同时,您的DatabaseManager 是一个单例。它的引用存储为一个静态 变量。它在 Activity 娱乐中幸存下来。它会一直存在,直到进程被终止。

因此,当您第二次运行 queryAllInsects 时,回调将发送到 MainActivityold 实例,这是不可见的不再。

您不应在 DatabaseManager 中保留对 MainActivity 的引用。这是一个内存泄漏,因为它不能被垃圾收集

关于android - 没有连接适配器;单击后退按钮并再次启动应用程序时跳过布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51001068/

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