简体   繁体   中英

android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment

Hello stackoverflow community, I have been facing with a little problem whole week. I'm trying to launch Map activity from my Main activity, and after calling Map Activity I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: eu.tinysolutions.tinytracker, PID: 12795
              java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.tinysolutions.tinytracker/eu.tinysolutions.tinytracker.MapsActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5728)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
               Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
                  at android.app.Activity.setContentView(Activity.java:2191)
                  at eu.tinysolutions.tinytracker.MapsActivity.onCreate(MapsActivity.java:20)
                  at android.app.Activity.performCreate(Activity.java:6309)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:207) 
                  at android.app.ActivityThread.main(ActivityThread.java:5728) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
               Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412) 
                  at android.app.Activity.setContentView(Activity.java:2191) 
                  at eu.tinysolutions.tinytracker.MapsActivity.onCreate(MapsActivity.java:20) 
                  at android.app.Activity.performCreate(Activity.java:6309) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:207) 
                  at android.app.ActivityThread.main(ActivityThread.java:5728) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
               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.q.b(:com.google.android.gms.dynamite_dynamitemodulesb@12673019@12.6.73 (040300-194189626):40)
                  at com.google.maps.api.android.lib6.auth.e.a(:com.google.android.gms.dynamite_dynamitemodulesb@12673019@12.6.73 (040300-194189626):11)
                  at com.google.maps.api.android.lib6.impl.e.a(:com.google.android.gms.dynamite_dynamitemodulesb@12673019@12.6.73 (040300-194189626):4)
                  at com.google.android.gms.maps.internal.b.a(:com.google.android.gms.dynamite_dynamitemodulesb@12673019@12.6.73 (040300-194189626):24)
                  at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms.dynamite_dynamitemodulesb@12673019@12.6.73 (040300-194189626):69)
                  at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.dynamite_dynamitemodulesb@12673019@12.6.73 (040300-194189626):32)
                  at com.google.android.gms.maps.internal.i.onTransact(:com.google.android.gms.dynamite_dynamitemodulesb@12673019@12.6.73 (040300-194189626):31)
                  at android.os.Binder.transact(Binder.java:392)
                  at com.google.android.gms.internal.maps.zza.transactAndReadException(Unknown Source)
                  at com.google.android.gms.maps.internal.zzf.zzc(Unknown Source)
                  at com.google.android.gms.maps.SupportMapFragment$zzb.zzc(Unknown Source)
                  at com.google.android.gms.maps.SupportMapFragment$zzb.createDelegate(Unknown Source)
                  at com.google.android.gms.dynamic.DeferredLifecycleHelper.zza(Unknown Source)
                  at com.google.android.gms.dynamic.DeferredLifecycleHelper.onInflate(Unknown Source)
                  at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
                  at android.support.v4.app.Fragment.onInflate(Fragment.java:1306)
                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3672)
                at android.support.v4.app.

MainActivity code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapInit();
}
public void mapInit(){
    startActivity(new Intent(this, MapsActivity.class));

}

MapsAcitivity is just a default android studio google maps activity:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

  private GoogleMap mMap;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
  }

I have found out that the app brakes on MapsActivity setContentView line

PS I've added API key to manifest file

UPDATE

sorry, I've forgotten to add layout files

Here is the activity_maps layout file:

<?xml version="1.0" encoding="utf-8"?>
<fragment 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" />

its there in the logs

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

add your API key in manifest file

eg

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    .
    .
    <application
          .  
          <meta-data
                 android:name="com.google.android.geo.API_KEY"
                 android:value="YOUR API KEY HERE" />
    </application>
</manifest>

The problem is, because you are using a fragment without a container view.

You can read about it from the Building a Flexible UI documentation :

An important rule when dealing with fragments—especially when adding fragments at runtime—is that your activity layout must include a container View in which you can insert the fragment.

And from this Fragment documentation :

When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity's view hierarchy and the fragment defines its own view layout. You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a element, or from your application code by adding it to an existing ViewGroup

So, add a root layout to your xml by changing your activity_maps layout to something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MapsActivity" >

      <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

Remember to use class instead of android:name attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM