I'm trying to get mainActivity context and mapView inside my fragment, I tried to write some code but when I write this:
contextMain = activity?.baseContext!!
mapView = view.findViewById<MapView>(R.id.map)
i get this error:
java.lang.NullPointerException: view.findViewById<MapView>(R.id.map) must not be null
this is my fragment code
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import org.osmdroid.views.MapView
// 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.
* Use the [CreatePathFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class CreatePathFragment() : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private lateinit var editTextLatitudineP1: EditText
private lateinit var editTextLongitudineP1: EditText
private lateinit var editTextLatitudineP2: EditText
private lateinit var editTextLongitudineP2: EditText
private lateinit var contextMain: Context
private lateinit var mapView: MapView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view: View = inflater.inflate(R.layout.fragment_create_path, container, false)
contextMain = activity?.baseContext!!
mapView = view.findViewById<MapView>(R.id.map)
// traccia percorso tra i punti degli editText
editTextLatitudineP1 = view.findViewById<EditText>(R.id.editTextLatitudineP1)
editTextLongitudineP1 = view.findViewById<EditText>(R.id.editTextLongitudineP1)
editTextLatitudineP2 = view.findViewById<EditText>(R.id.editTextLatitudineP2)
editTextLongitudineP2 = view.findViewById<EditText>(R.id.editTextLongitudineP2)
val b = view.findViewById<Button>(R.id.location)
b.setOnClickListener{
try {
Log.d("Percorso", "Premuto")
val p1lati: Double = editTextLatitudineP1.text.toString().toDouble()
val p1long: Double = editTextLongitudineP1.text.toString().toDouble()
val p2lati: Double = editTextLatitudineP2.text.toString().toDouble()
val p2long: Double = editTextLongitudineP2.text.toString().toDouble()
println("latitudine p1: $p1lati, longitudine p1: $p1long")
println("latitudine p2: $p2lati, longitudine p2: $p2long")
val navFun: NavFun = NavFun(contextMain, mapView)
navFun.routePath(p1lati, p1long, p2lati, p2long)
mapView.invalidate()
} catch (e: Exception) {
Toast.makeText(activity, "Errore nel inserimento delle coordinate", Toast.LENGTH_SHORT).show()
}
}
// Inflate the layout for this fragment
return view
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment CreatePathFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
CreatePathFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
this is my fragment xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".CreatePathFragment"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/crea_percorso"
android:textColor="@color/black"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="@ id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/inserisci_le_coordinate_dei_punti_tra_cui_vuoi_creare_un_percorso" />
<TextView
android:id="@ id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/latitudine_punto_di_partenza" />
<EditText
android:id="@ id/editTextLatitudineP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="@string/latitudine_p1"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="@ id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/longitudine_punto_di_partenza" />
<EditText
android:id="@ id/editTextLongitudineP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="@string/longitudine_p1"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="@ id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/latitudine_punto_di_arrivo" />
<EditText
android:id="@ id/editTextLatitudineP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="@string/latitudine_p2"
android:inputType="numberSigned|numberDecimal" />
<TextView
android:id="@ id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/longitudine_punto_di_arrivo" />
<EditText
android:id="@ id/editTextLongitudineP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="@string/longitudine_p2"
android:inputType="numberSigned|numberDecimal" />
<Button
android:id="@ id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/crea_percorso"
/>
</LinearLayout>
this is the error i have
E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.co.lorenzopulcinelli.navigationdrawer, PID: 21670
java.lang.NullPointerException: view.findViewById<MapView>(R.id.map) must not be null
at uk.co.lorenzopulcinelli.navigationdrawer.CreatePathFragment.onCreateView(CreatePathFragment.kt:54)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
I/Process: Sending signal. PID: 21670 SIG: 9
I have declared and displayed the map in the mainActivity xml file in this way:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.osmdroid.views.MapView
android:id="@ id/map"
tilesource="Mapnik"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="-181dp"
tools:layout_editor_absoluteY="57dp">
</org.osmdroid.views.MapView>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/frameLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@ id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu"
android:layout_gravity="start"
android:fitsSystemWindows="true"/>
</androidx.drawerlayout.widget.DrawerLayout>
CodePudding user response:
You defined R.id.map in the activity, but you are executing the findViewById on the fragment's view. For the fragment to find the map defined as part of the activity, you can try the following:
val map = activity?.findViewById<MapView>(R.id.map)
This will return null under 2 conditions: activity is null, or if map does not exist on the activity.