您好,我已经将我的 activity class 更新为片段,但我不确定如何更新照片中的部分
Hello, I have updated my activity class as fragment, but I am not sure how to update the parts in the photo
我已经将我的 activity class 更新为片段,但我不确定如何更新照片中的部分。
我的代码:
package com.nisaefendioglu.recentearthquakes.fragment
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.nisaefendioglu.recentearthquakes.R
import com.nisaefendioglu.recentearthquakes.RecyclerAdapter
import com.nisaefendioglu.recentearthquakes.model.EarthquakeModelItem
import com.nisaefendioglu.recentearthquakes.service.ApiClient
import kotlinx.android.synthetic.main.earthquake.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class Earthquake : Fragment((R.layout.earthquake)) {
private var listUsers: MutableList<EarthquakeModelItem> = mutableListOf<EarthquakeModelItem>()
private var adapter: RecyclerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
listUsers = mutableListOf()
recyclerview.layoutManager = LinearLayoutManager(this@Earthquake)
adapter = RecyclerAdapter(
this,
listUsers
)
recyclerview.adapter = adapter
getUsersData()
}
private fun getUsersData() {
ApiClient.apiService.getEarthquakes().enqueue(object :
Callback<MutableList<EarthquakeModelItem>> {
override fun onFailure(call: Call<MutableList<EarthquakeModelItem>>, t: Throwable) {
Log.e("error", t.localizedMessage)
}
override fun onResponse(
call: Call<MutableList<EarthquakeModelItem>>,
response: Response<MutableList<EarthquakeModelItem>>
) {
val usersResponse = response.body()
listUsers.clear()
usersResponse?.let { listUsers.addAll(it) }
adapter?.notifyDataSetChanged()
}
})
}
}
MainActivity.kt
package com.nisaefendioglu.recentearthquakes.view
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.nisaefendioglu.recentearthquakes.R
import com.nisaefendioglu.recentearthquakes.fragment.Earthquake
import com.nisaefendioglu.recentearthquakes.fragment.InfoFragment
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val info= InfoFragment()
val earthquake = Earthquake()
//val maps = MapFragment()
setCurrentFragment(info)
bottomNavigationView.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.infoFragment ->setCurrentFragment(info)
R.id.earthquakeFragment ->setCurrentFragment(earthquake)
// R.id.mapsFragment->setCurrentFragment(maps)
}
true
}
}
private fun setCurrentFragment(fragment: Fragment)=
supportFragmentManager.beginTransaction().apply {
replace(R.id.flFragment,fragment)
commit()
}
}
错误:
e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (26, 58): 类型不匹配:推断类型是 Earthquake 但 Context!预计
e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (29, 13): 类型不匹配:推断类型是地震,但上下文是预期的
我已经将我的 activity class 更新为片段,但我不确定如何更新照片中的部分。
你能帮帮我吗?
我已经将我的 activity class 更新为片段,但我不确定如何更新照片中的部分。
你能帮帮我吗?
- 只需将上下文作为 activity 或上下文传递,您不能将其用作上下文,因为您在片段中
使用 requireContext()
从片段中获取上下文。
如果你得到NullPointerException
,那是因为你在onCreate()
方法里面调用了recyclerView
,正如文档所说,
you can not rely on things like the activity's content view hierarchy being initialized at this point.
如果您使用片段,您应该始终在 onCreateView()
方法中调用您的 View
。
将 onCreate 部分更改为 onViewCreated 对我有用。
我已经将我的 activity class 更新为片段,但我不确定如何更新照片中的部分。
我的代码:
package com.nisaefendioglu.recentearthquakes.fragment
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.nisaefendioglu.recentearthquakes.R
import com.nisaefendioglu.recentearthquakes.RecyclerAdapter
import com.nisaefendioglu.recentearthquakes.model.EarthquakeModelItem
import com.nisaefendioglu.recentearthquakes.service.ApiClient
import kotlinx.android.synthetic.main.earthquake.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class Earthquake : Fragment((R.layout.earthquake)) {
private var listUsers: MutableList<EarthquakeModelItem> = mutableListOf<EarthquakeModelItem>()
private var adapter: RecyclerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
listUsers = mutableListOf()
recyclerview.layoutManager = LinearLayoutManager(this@Earthquake)
adapter = RecyclerAdapter(
this,
listUsers
)
recyclerview.adapter = adapter
getUsersData()
}
private fun getUsersData() {
ApiClient.apiService.getEarthquakes().enqueue(object :
Callback<MutableList<EarthquakeModelItem>> {
override fun onFailure(call: Call<MutableList<EarthquakeModelItem>>, t: Throwable) {
Log.e("error", t.localizedMessage)
}
override fun onResponse(
call: Call<MutableList<EarthquakeModelItem>>,
response: Response<MutableList<EarthquakeModelItem>>
) {
val usersResponse = response.body()
listUsers.clear()
usersResponse?.let { listUsers.addAll(it) }
adapter?.notifyDataSetChanged()
}
})
}
}
MainActivity.kt
package com.nisaefendioglu.recentearthquakes.view
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.nisaefendioglu.recentearthquakes.R
import com.nisaefendioglu.recentearthquakes.fragment.Earthquake
import com.nisaefendioglu.recentearthquakes.fragment.InfoFragment
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val info= InfoFragment()
val earthquake = Earthquake()
//val maps = MapFragment()
setCurrentFragment(info)
bottomNavigationView.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.infoFragment ->setCurrentFragment(info)
R.id.earthquakeFragment ->setCurrentFragment(earthquake)
// R.id.mapsFragment->setCurrentFragment(maps)
}
true
}
}
private fun setCurrentFragment(fragment: Fragment)=
supportFragmentManager.beginTransaction().apply {
replace(R.id.flFragment,fragment)
commit()
}
}
错误: e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (26, 58): 类型不匹配:推断类型是 Earthquake 但 Context!预计
e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (29, 13): 类型不匹配:推断类型是地震,但上下文是预期的
我已经将我的 activity class 更新为片段,但我不确定如何更新照片中的部分。 你能帮帮我吗?
我已经将我的 activity class 更新为片段,但我不确定如何更新照片中的部分。 你能帮帮我吗?
- 只需将上下文作为 activity 或上下文传递,您不能将其用作上下文,因为您在片段中
使用 requireContext()
从片段中获取上下文。
如果你得到NullPointerException
,那是因为你在onCreate()
方法里面调用了recyclerView
,正如文档所说,
you can not rely on things like the activity's content view hierarchy being initialized at this point.
如果您使用片段,您应该始终在 onCreateView()
方法中调用您的 View
。
将 onCreate 部分更改为 onViewCreated 对我有用。