1. Are you ready for the Galaxy S20? Here is everything we know so far!

get error kotlin null pointer exception when switching fragment

Discussion in 'Android Development' started by anwartan, May 8, 2020.

  1. anwartan

    anwartan Lurker
    Thread Starter

    I have given the error section with an underscore


    import android.app.SearchManager
    import android.content.Context
    import android.os.Bundle
    import android.util.Log
    import android.view.*
    import androidx.appcompat.widget.SearchView
    import androidx.core.content.getSystemService
    import androidx.fragment.app.Fragment
    import androidx.recyclerview.widget.LinearLayoutManager
    import androidx.recyclerview.widget.RecyclerView
    import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

    import com.example.paduma.R
    import com.example.paduma.model.DataApi
    import com.example.paduma.model.Paritta
    import com.example.paduma.network.ParittaApi
    import com.example.paduma.network.RetrofitClientInstance
    import retrofit2.Call
    import retrofit2.Callback
    import retrofit2.Response
    import java.io.IOException

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private const val ARG_PARAM1 = "param1"
    private const val ARG_PARAM2 = "param2"

    /**
    * A simple [Fragment] subclass.
    *
    */
    class ParittaFragment : Fragment() {

    companion object {
    val INSTANCE_STATE = "INSTANCE_STATE"
    }

    override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
    ): View? {
    setHasOptionsMenu(true)
    Log.d("life","createview")
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_paritta, container, false)
    }

    private lateinit var swipeRefreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView
    private lateinit var linearLayoutManager: LinearLayoutManager
    private lateinit var parittaApi: ParittaApi
    private lateinit var adapterParitta: AdapterParitta
    private var dataParitta: ArrayList<Paritta.DataParitta>? = ArrayList()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    // if(savedInstanceState!=null) {
    // if (savedInstanceState.getParcelableArrayList<Paritta.DataParitta>(INSTANCE_STATE) != null) {
    // dataParitta = savedInstanceState.getParcelableArrayList(INSTANCE_STATE)!!
    // setDataRecycler(dataParitta)
    // }
    // }else{
    // loadRecyclerViewData()
    //
    // }
    init(view)
    Log.d("life","viewcreated")


    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    Log.d("life","activityCreated")

    loadData()
    }

    private fun init(view: View) {
    swipeRefreshLayout = view.findViewById(R.id.swipeLayout)
    parittaApi = RetrofitClientInstance().getRetrofitInstance().create(ParittaApi::class.java)
    recyclerView = view.findViewById(R.id.recycler_paritta)
    linearLayoutManager = LinearLayoutManager(view.context)
    recyclerView.layoutManager = linearLayoutManager


    }


    private fun loadData() {
    swipeRefreshLayout.isRefreshing=true
    val call = parittaApi.getAllParitta()
    call.enqueue(object : Callback<Paritta> {
    override fun onFailure(call: Call<Paritta>, t: Throwable) {

    }

    override fun onResponse(call: Call<Paritta>, response: Response<Paritta>) {
    if (!response.isSuccessful) {
    return
    }
    dataParitta = response.body()!!.data

    adapterParitta =
    AdapterParitta(dataParitta as ArrayList<DataApi>, R.layout.recycler_paritta, context!!)

    recyclerView.adapter = adapterParitta
    swipeRefreshLayout.isRefreshing=true

    }

    })
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.main_menu, menu)
    val searchView = menu.findItem(R.id.action_search).actionView as SearchView
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
    override fun onQueryTextSubmit(query: String?): Boolean {
    return false
    }

    override fun onQueryTextChange(newText: String?): Boolean {
    adapterParitta.filter.filter(newText)
    return false
    }

    })
    super.onCreateOptionsMenu(menu, inflater)
    }


    // override fun onSaveInstanceState(outState: Bundle) {
    // super.onSaveInstanceState(outState)
    // outState.putParcelableArrayList(INSTANCE_STATE,dataParitta)
    // }


    }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - error kotlin null
  1. brentc
    Replies:
    1
    Views:
    227
  2. Mohammed Khalid
    Replies:
    5
    Views:
    487
  3. erenalkan
    Replies:
    5
    Views:
    497
  4. Boris_yo
    Replies:
    1
    Views:
    479
  5. willsp28
    Replies:
    1
    Views:
    286
  6. Danilo Pazzelli
    Replies:
    1
    Views:
    523
  7. unet
    Replies:
    0
    Views:
    524
  8. codezer0
    Replies:
    8
    Views:
    735
  9. Yesi Trotamundos
    Replies:
    6
    Views:
    865
  10. raksheg
    Replies:
    0
    Views:
    1,505

Share This Page

Loading...