Android 为什么底部导航抽屉片段显示空数据
我正在开发新闻应用程序,我已经创建了带有片段的底部导航,在topheadlinesfragment中,我正在使用改型获得新闻数据,但它显示的是空的白色屏幕 在下面 下面是我的主要活动,在这里我用片段实现了bottomnavigationdrawerAndroid 为什么底部导航抽屉片段显示空数据,android,android-fragments,android-recyclerview,bottomnavigationview,Android,Android Fragments,Android Recyclerview,Bottomnavigationview,我正在开发新闻应用程序,我已经创建了带有片段的底部导航,在topheadlinesfragment中,我正在使用改型获得新闻数据,但它显示的是空的白色屏幕 在下面 下面是我的主要活动,在这里我用片段实现了bottomnavigationdrawer class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(s
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom)
bottomNavigationView.setOnNavigationItemSelectedListener {
var selectedFragment = Fragment()
when (it.itemId) {
R.id.top_headline -> selectedFragment = TopHeadlinesFragment()
R.id.espn_news -> selectedFragment = ESPNFragment()
R.id.bbc_sport -> selectedFragment = BBCSportFragment()
R.id.football_italia -> selectedFragment = FootballItaliaFragment()
}
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame_layout, selectedFragment)
transaction.commit()
return@setOnNavigationItemSelectedListener true
}
}
}
“E/RecyclerView:未连接适配器;”您未初始化适配器
我不确定它是否能解决您的问题,但首先将布局更改为:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_gravity="bottom"
app:menu="@menu/bottomnews_nav"
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
“E/RecyclerView:未连接适配器;”您没有初始化适配器
我不确定它是否能解决您的问题,但首先将布局更改为:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_gravity="bottom"
app:menu="@menu/bottomnews_nav"
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
你的问题是你的回收视图没有出现在头条标题上。
在
TopHeadlinesFragment
class TopHeadlinesFragment : Fragment() {
var topHeadlinesAdapter: TopHeadlinesAdapter? = null
//3
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(
R.layout.fragment_top_headlines
, container, false
)
val recyclerView = view.findViewById (R.id.recyclerView) as RecyclerView
topHeadlinesAdapter = TopHeadlinesAdapter(recyclerView.context)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = topHeadlinesAdapter
val apiInterface = SportNewsInterface.create().getNews()
apiInterface.enqueue(object : Callback<SportNewsResponse> {
override fun onResponse(
call: Call<SportNewsResponse>?,
response: Response<SportNewsResponse>?
) {
if (response!!.body() != null) {
topHeadlinesAdapter!!.setMovieListItems(response.body()!!.articles)
}
}
override fun onFailure(call: Call<SportNewsResponse>?, t: Throwable?) {
}
})
return view
}
}
class-TopHeadlinesFragment:Fragment(){
变量topHeadlinesAdapter:topHeadlinesAdapter?=null
//3
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
val视图=充气机。充气(
R.layout.fragment_顶部标题
,货柜,虚假
)
val recyclerView=view.findviewbyd(R.id.recyclerView)作为recyclerView
topHeadlinesAdapter=topHeadlinesAdapter(recyclerView.context)
recyclerView.layoutManager=LinearLayoutManager(上下文)
recyclerView.adapter=topHeadlinesAdapter
val apiInterface=SportNewsInterface.create().getNews()
排队(对象:回调{
覆盖响应(
呼叫:呼叫?,
回应:回应?
) {
if(response!!.body()!=null){
topHeadlinesAdapter!!.setMovieListItems(response.body()!!.articles)
}
}
覆盖失效时的乐趣(调用:调用?、t:可丢弃?){
}
})
返回视图
}
}
你的问题是你的回收视图没有出现在头条标题上。
在
TopHeadlinesFragment
class TopHeadlinesFragment : Fragment() {
var topHeadlinesAdapter: TopHeadlinesAdapter? = null
//3
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(
R.layout.fragment_top_headlines
, container, false
)
val recyclerView = view.findViewById (R.id.recyclerView) as RecyclerView
topHeadlinesAdapter = TopHeadlinesAdapter(recyclerView.context)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = topHeadlinesAdapter
val apiInterface = SportNewsInterface.create().getNews()
apiInterface.enqueue(object : Callback<SportNewsResponse> {
override fun onResponse(
call: Call<SportNewsResponse>?,
response: Response<SportNewsResponse>?
) {
if (response!!.body() != null) {
topHeadlinesAdapter!!.setMovieListItems(response.body()!!.articles)
}
}
override fun onFailure(call: Call<SportNewsResponse>?, t: Throwable?) {
}
})
return view
}
}
class-TopHeadlinesFragment:Fragment(){
变量topHeadlinesAdapter:topHeadlinesAdapter?=null
//3
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
val视图=充气机。充气(
R.layout.fragment_顶部标题
,货柜,虚假
)
val recyclerView=view.findviewbyd(R.id.recyclerView)作为recyclerView
topHeadlinesAdapter=topHeadlinesAdapter(recyclerView.context)
recyclerView.layoutManager=LinearLayoutManager(上下文)
recyclerView.adapter=topHeadlinesAdapter
val apiInterface=SportNewsInterface.create().getNews()
排队(对象:回调{
覆盖响应(
呼叫:呼叫?,
回应:回应?
) {
if(response!!.body()!=null){
topHeadlinesAdapter!!.setMovieListItems(response.body()!!.articles)
}
}
覆盖失效时的乐趣(调用:调用?、t:可丢弃?){
}
})
返回视图
}
}
您是否收到任何异常或警告?你从你的API得到响应了吗?Maor Hadad我从我使用PostAnalyso检查的API得到响应,所以为片段添加一个容器。frame_layout是主布局,在其中添加一个新的framelayout以包含碎片,但是你在应用程序中得到了它吗?请验证它。如果您将代码上传到bitbucket或github,这样我也可以运行它,这会很有帮助。您的意思是,在片段内部,我不明白。您是否收到任何异常或警告?你从你的API得到响应了吗?Maor Hadad我从我使用PostAnalyso检查的API得到响应,所以为片段添加一个容器。frame_layout是主布局,在其中添加一个新的framelayout以包含碎片,但是你在应用程序中得到了它吗?请验证它。如果您将代码上传到bitbucket或github,这样我也可以运行它,这会很有帮助。您的意思是在片段内部我不明白。我已经添加了log@MaorHadad请检查它。我已经添加了log@MaorHadad请检查它
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2019-10-10 15:23:07.818 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:08.324 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:09.497 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:12.277 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:14.761 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:21.146 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
2019-10-10 15:23:21.452 27558-27558/yodgorbek.komilov.musobaqayangiliklari E/RecyclerView: No adapter attached; skipping layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_gravity="bottom"
app:menu="@menu/bottomnews_nav"
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
class TopHeadlinesFragment : Fragment() {
var topHeadlinesAdapter: TopHeadlinesAdapter? = null
//3
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(
R.layout.fragment_top_headlines
, container, false
)
val recyclerView = view.findViewById (R.id.recyclerView) as RecyclerView
topHeadlinesAdapter = TopHeadlinesAdapter(recyclerView.context)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = topHeadlinesAdapter
val apiInterface = SportNewsInterface.create().getNews()
apiInterface.enqueue(object : Callback<SportNewsResponse> {
override fun onResponse(
call: Call<SportNewsResponse>?,
response: Response<SportNewsResponse>?
) {
if (response!!.body() != null) {
topHeadlinesAdapter!!.setMovieListItems(response.body()!!.articles)
}
}
override fun onFailure(call: Call<SportNewsResponse>?, t: Throwable?) {
}
})
return view
}
}