Reputation: 35
I'm trying to add a map fragment to an activity much like the get started guide: https://developers.google.com/maps/documentation/android-sdk/start
My problem is that the app just quits the moment it's run. So for the relevant parts of the MainActivity.java file:
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
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;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Also the (hopefully relevant parts) of activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="363dp"
android:layout_height="252dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toBottomOf="@+id/rcv"
tools:context=".MainActivityy" />
</androidx.constraintlayout.widget.ConstraintLayout>
Apart from this i have also created "google_maps_api.xml" in the values folder (real api redacted):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">MYGOOGLEAPI</string>
</resources>
I've managed to verify my api working through following the previously linked example to the letter, in my own application it does not work at all however.
I've managed to identify the following error in "logcat":
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #78 in com.example.myapplication:layout/activity_main: Binary XML file line #78 in com.example.myapplication:layout/activity_main: Error inflating class fragment
Thus to my understanding my problem lies in the activity_main.xml file, what it means or how to solve it puzzles me unfortunatly. Any help is greatly appreciated. Just to clearify, line 78 is hte line of "
Another idea is that i perhaps have to initialise/declare it like a widget?
including full Logcat in case prevously mentioned is nothing critical:
2019-10-08 20:45:20.514 7610-7610/? E/e.myapplicatio: Unknown bits set in runtime_flags: 0x8000
2019-10-08 20:45:21.717 7610-7610/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 7610
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #78 in com.example.myapplication:layout/activity_main: Binary XML file line #78 in com.example.myapplication:layout/activity_main: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.view.InflateException: Binary XML file line #78 in com.example.myapplication:layout/activity_main: Binary XML file line #78 in com.example.myapplication:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #78 in com.example.myapplication:layout/activity_main: Error inflating class fragment
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.b(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-256199907):22)
at com.google.maps.api.android.lib6.auth.d.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-256199907):10)
at com.google.maps.api.android.lib6.impl.d.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-256199907):41)
at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-256199907):61)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-256199907):25)
at com.google.android.gms.maps.internal.h.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-256199907):73)
at hp.onTransact(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-256199907):4)
at android.os.Binder.transact(Binder.java:914)
at com.google.android.gms.internal.maps.zza.zza(Unknown Source:10)
at com.google.android.gms.maps.internal.zzf.zzc(Unknown Source:5)
at com.google.android.gms.maps.SupportMapFragment$zzb.zzd(Unknown Source:12)
at com.google.android.gms.maps.SupportMapFragment$zzb.createDelegate(Unknown Source:6)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.zaa(Unknown Source:18)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.onInflate(Unknown Source:20)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source:21)
at androidx.fragment.app.Fragment.onInflate(Fragment.java:1384)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3744)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:120)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
2019-10-08 20:45:21.718 7610-7610/com.example.myapplication E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:438)
at android.app.Activity.setContentView(Activity.java:3324)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Upvotes: 1
Views: 826
Reputation: 2343
Try to add google map api key inside tag in android manifest file.
<application>
//...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
//...
</application>
Upvotes: 1
Reputation: 1271
Add meta data with your generated API key In your manifest file with the API key you have generated.
Upvotes: 1
Reputation: 11
Try to add your Google Maps API Key to your AndroidManifest.xml as suggested in the stack trace in the following line:
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
Upvotes: 1