Android:检视传回null

时间:2019-03-14 06:41:00

标签: android android-fragments kotlin mvp rxpermissions

我正在尝试获取联系人并将其显示在列表中。我为此使用RxPermissions。这里的问题是,当我第一次允许该权限时,即使列表中有项目,视图也将返回null并显示空列表。但是,当我返回上一个活动并返回时,它工作正常。

ContactsFragment.kt

override fun onResume() {
        super.onResume()
        if (isPermissionGranted) {
            Timber.d("Permission is granted")
            contactsPresenter.fetchContacts(context)
            noPermissionsWarning.visibility = View.GONE
        } else {
            noPermissionsWarning.visibility = View.VISIBLE
            showContactList(ArrayList()) // Show empty list when permissions not granted
        }
    }
private fun askPermissions() {
    rxPermissions
            .request(android.Manifest.permission.GET_ACCOUNTS,
                    android.Manifest.permission.READ_CONTACTS,
                    android.Manifest.permission.WRITE_CONTACTS)
            .subscribe { permissionGranted ->
                isPermissionGranted = permissionGranted
            }
}



override fun showContactList(selectedContacts: ArrayList<Contact>) {
        contactAdapter = ContactListAdapter(selectedContacts, this, context)
        val layoutManager = LinearLayoutManager(this.context)
        contactsRecyclerView?.layoutManager = layoutManager
        contactsRecyclerView?.addItemDecoration(DividerItemDecoration(
                contactsRecyclerView?.context,
                DividerItemDecoration.VERTICAL
        ))
        contactsRecyclerView?.adapter = contactAdapter
    }

ContactsPresenter.kt

override fun takeView(view: ContactsContract.View, context: Context?) {
        super.takeView(view)
        contactList.clear()
        tempContactList.clear()
    }

    override fun fetchContacts(context: Context?) {
        contactManager.getContacts(context)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    it.let {
                        tempContactList.addAll(it.sortedWith(compareBy { it.userName }))
                        contactList.addAll(it.sortedWith(compareBy { it.userName }))
                        view?.showContactList(contactList)// this view is null at first
                    }
                }, {

                }).addDisposableTo(disposable)
    }

我在这里会缺少什么。

谢谢

3 个答案:

答案 0 :(得分:2)

您似乎应该在此行之后立即获取联系人:

isPermissionGranted = permissionGranted

答案 1 :(得分:1)

在检查是否授予许可时调用您的方法

 if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == 
                      PackageManager.PERMISSION_GRANTED) {
Filtering filtering=new Filtering();
//contactFetch method  here
    } else {         

     Permission.requestStoragePermission(MainActivity.this,
                    STORAGE_PERMISSION_CODE);
    }

之后,当您寻求许可时……

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == STORAGE_PERMISSION_CODE)  {
        if (grantResults.length > 0 && grantResults[0] == 
                   PackageManager.PERMISSION_GRANTED) {
            Filtering filtering=new Filtering();
            //Again your fetching method
        } else {
            Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
            Permission.requestStoragePermission(MainActivity.this,
                                  STORAGE_PERMISSION_CODE);
        }
    }
}

我不确定是否可以尝试此操作,但我认为它将为您提供帮助

答案 2 :(得分:0)

我通过在onResume()onViewCreated()内都附加了视图来解决它。谢谢大家的帮助

  • 经过很长一段时间,我才发现自己正在清理onPause()上的一次性物品,本来应该在onDestroy()内部完成。我写了解决方案以防万一。