我正在构建一个应用程序,它从Main Activity
上的API返回Github用户列表,并在Detail User Activity
上返回所选用户的详细信息。当我运行应用程序时,Main Activity
使用RecyclerView
显示列表,但当我按下其中一个用户时,我的应用程序移动到该Activity,但不会显示任何内容。同样在我的Logcat中,有一个错误说No adapters attached,skipping layout.即使我没有任何RecyclerView
在我的详细活动.
下面是我的MainActivity
:
package com.example.githubuser
import android.app.SearchManager
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.githubuser.databinding.ActivityMainBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.ViewModelProvider
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var mainViewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val mainViewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(MainViewModel::class.java)
mainViewModel.listUsers.observe(this) { listUsers ->
val adapter = UsersAdapter(listUsers)
binding.rvUsers.adapter = adapter
val layoutManager = LinearLayoutManager(this)
binding.rvUsers.layoutManager = layoutManager
}
mainViewModel.isLoading.observe(this) {
showLoading(it)
}
}
private fun showUsers(listUsers: List<ItemsItem>) {
val adapter = UsersAdapter(listUsers)
binding.rvUsers.adapter = adapter
val layoutManager = LinearLayoutManager(this)
binding.rvUsers.layoutManager = layoutManager
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.option_menu, menu)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val searchView = menu.findItem(R.id.search).actionView as SearchView
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName))
searchView.queryHint = resources.getString(R.string.search_hint)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(que: String): Boolean {
showLoading(true)
getUserSearchData(query = que)
searchView.clearFocus()
return true
}
override fun onQueryTextChange(newText: String): Boolean {
return false
}
})
return true
}
private fun getUserSearchData (query : String){
ApiConfig.getApiService().getUsers(query = query).enqueue(object : Callback<GithubResponse>{
override fun onResponse(
call: Call<GithubResponse>,
response: Response<GithubResponse>
) {
showLoading(false)
if (response.isSuccessful){
val filteredUsers = response.body()?.items?.filter { it.login.contains(query, true) }
showUsers(filteredUsers ?: emptyList())
}else{
Log.e("MainViewModel", "onFailure: ${response.message()}")
}
}
override fun onFailure(call: Call<GithubResponse>, t: Throwable) {
showLoading(false)
Log.d("TAG", "${t.message}")
}
})
}
private fun showLoading(isLoading: Boolean) {
binding.progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
}
}
下面是我的UsersAdapter
:
package com.example.githubuser
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class UsersAdapter(private var listUsers: List<ItemsItem>): RecyclerView.Adapter<UsersAdapter.MyViewHolder>() {
companion object {
const val EXTRA_USER = "user"
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int) =
MyViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.item_users, viewGroup, false))
override fun onBindViewHolder(myViewHolder: MyViewHolder, position: Int) {
val user = listUsers[position]
myViewHolder.tvUser.text = user.login
Glide.with(myViewHolder.ivUser)
.load(listUsers[position].avatarUrl)
.into(myViewHolder.ivUser)
myViewHolder.itemView.setOnClickListener {
val intent = Intent(myViewHolder.itemView.context, DetailUserActivity::class.java)
val userResponse = UserResponse(
gistsUrl = "",
reposUrl = "",
followingUrl = "",
createdAt = "",
login = user.login,
type = "",
blog = "",
subscriptionsUrl = "",
updatedAt = "",
siteAdmin = false,
company = "",
id = user.id,
publicRepos = 0,
gravatarId = "",
organizationsUrl = "",
starredUrl = "",
followersUrl = "",
publicGists = 0,
url = "",
receivedEventsUrl = "",
followers = 0,
avatarUrl = user.avatarUrl,
eventsUrl = "",
htmlUrl = "",
following = 0,
name = "",
location = "",
nodeId = ""
)
intent.putExtra(UsersAdapter.EXTRA_USER, userResponse)
intent.putExtra("photo", user.avatarUrl)
myViewHolder.itemView.context.startActivity(intent)
}
}
override fun getItemCount() = listUsers.size
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvUser: TextView = view.findViewById(R.id.tv_users)
val ivUser: ImageView = view.findViewById(R.id.iv_users)
}
}
下面是我的DetailUserActivity
:
package com.example.githubuser
import DetailViewModel
import android.media.Image
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.example.githubuser.UsersAdapter.Companion.EXTRA_USER
import com.example.githubuser.databinding.ActivityMainBinding
import org.w3c.dom.Text
class DetailUserActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var adapter: UsersAdapter
companion object {
const val EXTRA_NAME = "extra_name"
const val EXTRA_USERNAME = "extra_username"
const val EXTRA_FOLLOWERS = "extra_followers"
const val EXTRA_FOLLOWING = "extra_following"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val viewModel = ViewModelProvider(this)[DetailViewModel::class.java]
// adapter = UsersAdapter(ItemsItem)
val tv_name = findViewById<TextView>(R.id.name)
val avatar_user = findViewById<ImageView>(R.id.userPhoto)
val tv_username = findViewById<TextView>(R.id.username)
val tv_followers = findViewById<TextView>(R.id.followers)
val tv_following = findViewById<TextView>(R.id.following)
val user = intent.getParcelableExtra<UserResponse>("user_key")
val login = intent.getStringExtra(EXTRA_USERNAME)
if (login != null) {
viewModel.getDetailUser(login)
}
viewModel.userDetail.observe(this, Observer {
if (user != null){
showLoading(false)
Glide.with(this)
.load(user.avatarUrl)
.into(avatar_user)
tv_name.text = user.name
tv_username.text = user.login
// tv_company.text = user.company
// tv_location.text = user.location
tv_followers.text = user.followers.toString()
tv_following.text = user.following.toString()
}
})
}
private fun showLoading(isLoading: Boolean) {
binding.progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
}
}
第一节第一节第一节第一节第一次
我试着在其他问题上寻找这个特定的错误,但我似乎找不到答案。
这是我的堆栈跟踪错误(注意,我在结尾还有一个关于Glide模块的额外警告):第一节第二节第一节第三节第一节
1条答案
按热度按时间falq053o1#
也许你的布局有问题
这一行在你的Detail activity中有。它应该是ActivityDetailBinding,或者别的什么。你可以尝试
并请张贴您的堆栈跟踪错误
编辑:
在你的适配器中,你把2个额外的东西放在你的意图上:
但是,在细节活动中,您使用错误的键获得值:
由于您从Intent Extra获取用户模型,因此可以使用该数据更新UI-