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

ant*_*009 10 android recycler-adapter android-recyclerview

Android Studio 3.2 Canary 18
kotlin_version = 1.2.50
Run Code Online (Sandbox Code Playgroud)

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

我从sqlite加载数据,每次都加载数据.因为它填充了insectDataModelList.

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

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

我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

数据库我使用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();
                    }
                });
    }
}
Run Code Online (Sandbox Code Playgroud)

当应用程序第一次安装时,一切都按预期工作.如果你把它清除掉了.然而,它只有当你点击后退按钮,然后尝试并启动它不会加载,因为mAdapter在RecyclerView类是空的任何数据的应用程序.

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

在此输入图像描述

更新了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();
                    }
                });
    }
}
Run Code Online (Sandbox Code Playgroud)

非常感谢任何建议,

dev*_*max 6

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

与此同时,你DatabaseManager是一个单身人士.它的引用存储为静态变量.它在活动娱乐中幸存下来.它将一直存在直到该过程被杀死.

因此,当您queryAllInsects第二次运行时,回调将被发送到实例MainActivity,该实例不再可见.

你不应该参考保持到MainActivityDatabaseManager.这是一个内存泄漏,因为它不能被垃圾收集.