0

I have updated my activity class as fragment, but I am not sure how to update the parts in the photo.

photo

My Code :

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()
        }

}

Error : e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (26, 58): Type mismatch: inferred type is Earthquake but Context! was expected

e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (29, 13): Type mismatch: inferred type is Earthquake but Context was expected

I have updated my activity class as fragment, but I am not sure how to update the parts in the photo. Can you help me?

I have updated my activity class as fragment, but I am not sure how to update the parts in the photo. Can you help me?

Nisa Efendioglu
  • 901
  • 3
  • 12
  • 22

3 Answers3

0
  • Just pass context as activity or context you can't use this as context because you are inside a fragment
Manjeet deswal
  • 692
  • 2
  • 5
  • 18
0

Use requireContext() to get the context from fragment.

If you get NullPointerException, that is because you call the recyclerView inside onCreate() method , as the document said,

you can not rely on things like the activity's content view hierarchy being initialized at this point.

If you are using a fragment , you should always call your View in the onCreateView() method.

Muhammad Rio
  • 759
  • 5
  • 6
  • When I use it like that, it gives the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference – Nisa Efendioglu Apr 10 '22 at 14:59
  • @NisaEfendioglu you can fix it by defining your layout manager in the xml `app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"` – Muhammad Rio Apr 10 '22 at 15:03
  • it didn't work :( – Nisa Efendioglu Apr 10 '22 at 15:22
  • @NisaEfendioglu what error ?? still the same ?? – Muhammad Rio Apr 10 '22 at 15:23
  • java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference at com.nisaefendioglu.recentearthquakes.fragment.Earthquake.onCreate(Earthquake.kt:28) – Nisa Efendioglu Apr 10 '22 at 15:24
  • 28 -> recyclerview.layoutManager = LinearLayoutManager(context) – Nisa Efendioglu Apr 10 '22 at 15:25
  • @NisaEfendioglu delete `recyclerview.layoutManager = LinearLayoutManager(context)` if you already define your layout manager in the xml. – Muhammad Rio Apr 10 '22 at 15:29
  • 1
    The `NullPointerException` is not saying you need to set a `LayoutManager`. It is saying you are attempting to set a `LayoutManager` on a null reference, meaning the `RecyclerView`; it is null. In your line of code for your fragment (`recyclerview.layoutManager = LinearLayoutManager(this@Earthquake)`), I do not see where you instantiate `recyclerview`. – Flash103 Apr 10 '22 at 15:29
  • @NisaEfendioglu and move your code inside `onCreate()` to `onCreateView()`. – Muhammad Rio Apr 10 '22 at 15:35
  • @Flash103 he is using `Kotlin`. He need not do `findViewById()` – Sambhav Khandelwal Apr 10 '22 at 16:22
  • Changing the onCreate part to onViewCreated worked for me. Thank you all. :) – Nisa Efendioglu Apr 10 '22 at 18:58
0

Changing the onCreate part to onViewCreated worked for me.

Nisa Efendioglu
  • 901
  • 3
  • 12
  • 22