Home > Software design >  I am getting error in my Logcat and my app is not running its crashing, Help me please
I am getting error in my Logcat and my app is not running its crashing, Help me please

Time:09-12

everything below given are all the code to create a find the current location in google map.But its not working i am getting all the error and my app is not even opening

But in a You tube tutorial this code worked for the person who made that vedio.

I have tried a lot to fix but can't understand why it's not working.

My LogCat error

Caused by: android.view.InflateException: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Error inflating class androidx.fragment.app.FragmentContainerView
     
Caused by: android.view.InflateException: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Error inflating class androidx.fragment.app.FragmentContainerView
    
Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

Full Logcat Error

2022-09-12 13:45:57.822 13211-13211/com.example.addressfinder E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.addressfinder, PID: 13211
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.addressfinder/com.example.addressfinder.MapsActivity}: android.view.InflateException: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Error inflating class androidx.fragment.app.FragmentContainerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3479)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3651)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2104)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:7861)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
     Caused by: android.view.InflateException: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: android.view.InflateException: Binary XML file line #2 in com.example.addressfinder:layout/activity_maps: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
        at com.google.maps.api.android.lib6.drd.p.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150400-0):6)
        at com.google.maps.api.android.lib6.auth.f.i(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150400-0):1)
        at com.google.maps.api.android.lib6.impl.l.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150400-0):24)
        at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150400-0):7)
        at com.google.android.gms.maps.internal.CreatorImpl.logInitialization(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150400-0):3)
        at com.google.android.gms.maps.internal.g.ba(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150400-0):5)
        at en.onTransact(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150400-0):4)
        at android.os.Binder.transact(Binder.java:1043)
        at com.google.android.gms.internal.maps.zza.zzc(com.google.android.gms:play-services-maps@@18.1.0:2)
        at com.google.android.gms.maps.internal.zze.zzl(com.google.android.gms:play-services-maps@@18.1.0:4)
        at com.google.android.gms.maps.MapsInitializer.initialize(com.google.android.gms:play-services-maps@@18.1.0:12)
        at com.google.android.gms.maps.MapsInitializer.initialize(com.google.android.gms:play-services-maps@@18.1.0:1)
        at com.google.android.gms.maps.zzav.zzc(com.google.android.gms:play-services-maps@@18.1.0:2)
        at com.google.android.gms.maps.zzav.createDelegate(com.google.android.gms:play-services-maps@@18.1.0:1)
        at com.google.android.gms.dynamic.DeferredLifecycleHelper.zaf(com.google.android.gms:play-services-base@@18.0.1:6)
        at com.google.android.gms.dynamic.DeferredLifecycleHelper.onCreate(com.google.android.gms:play-services-base@@18.0.1:1)
        at com.google.android.gms.maps.SupportMapFragment.onCreate(com.google.android.gms:play-services-maps@@18.1.0:5)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
2022-09-12 13:45:57.823 13211-13211/com.example.addressfinder E/AndroidRuntime:     at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
        at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1071)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:999)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:661)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:536)
        at com.example.addressfinder.databinding.ActivityMapsBinding.inflate(ActivityMapsBinding.java:42)
        at com.example.addressfinder.databinding.ActivityMapsBinding.inflate(ActivityMapsBinding.java:36)
        at com.example.addressfinder.MapsActivity.onCreate(MapsActivity.kt:35)
        at android.app.Activity.performCreate(Activity.java:8109)
        at android.app.Activity.performCreate(Activity.java:8083)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3452)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3651)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2104)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:7861)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

MY MainActivity

package com.example.addressfinder

import android.content.pm.PackageManager
import android.location.Geocoder
import android.location.Location
import android.location.LocationRequest
import android.media.MediaParser.create
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.app.ActivityCompat

import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.example.addressfinder.databinding.ActivityMapsBinding
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import java.net.URI.create
import java.util.*

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap
    var fusedLocationProviderClient: FusedLocationProviderClient?=null
     private lateinit var binding: ActivityMapsBinding
    var currentLocation: Location?=null
    var permissionCode=101

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMapsBinding.inflate(layoutInflater)
        setContentView(binding.root)
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.

        fetchLocation()
    }

    private fun fetchLocation() {
        if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),permissionCode)
            return
        }
        val task= fusedLocationProviderClient?.lastLocation
        task?.addOnSuccessListener { location->
            if(location!=null)
            {
               currentLocation=location
                Toast.makeText(applicationContext,
                    currentLocation!!.latitude.toString() ""  currentLocation!!.longitude.toString(),Toast.LENGTH_LONG).show()
                val mapFragment = supportFragmentManager
                    .findFragmentById(R.id.map) as SupportMapFragment
                mapFragment.getMapAsync(this)

            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when(requestCode){
            permissionCode->if(grantResults.isNotEmpty() && grantResults[0]==PackageManager.PERMISSION_GRANTED)
                fetchLocation()
        }
    }

    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap
        val latlong=LatLng(currentLocation?.latitude!!,currentLocation?.longitude!!)
        drawMarker(latlong)
    }

    private fun drawMarker(latlong: LatLng) {
      val markerOption=MarkerOptions().position(latlong).title("I am Here")

        mMap.animateCamera((CameraUpdateFactory.newLatLng(latlong)))
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlong,20f))
        mMap.addMarker(markerOption)
    }

My activity_map.xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />

ActivityManifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.addressfinder">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AddressFinder"
        tools:targetApi="31">

        <!--
             TODO: Before you run your application, you need a Google Maps API key.

             To get one, follow the directions here:

                https://developers.google.com/maps/documentation/android-sdk/get-api-key

             Once you have your API key (it starts with "AIza"), define a new property in your
             project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the
             "YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
        -->
        <meta-data
            android:name="com.google.android.geo.AIzaSyDikbOQwPkf-SCQl3DbELX_o_2O2I5qCf0"
            android:value="@string/maps_Api" />

        <activity
            android:name=".MapsActivity"
            android:exported="true"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

CodePudding user response:

Add this code in your AndroidManifest.xml file

with

Google Maps Android API ENABLE

 <meta-data android:name="com.google.android.maps.v2.API_KEY"
    android:value="YOUR_MAP_API" />

or

Google Maps Geolocation API ENABLE

<meta-data android:name="com.google.android.geo.API_KEY" 
android:value="YOUR_GEO_API"/>
  • Related