user268397
user268397

Reputation: 1927

Android - Error Inflating Class Fragment - XML File

I'm getting an error with my SupportFragment code. It's having issues inflating the XML. It crashes when the activity starts. Here is the Java code I'm using:

@Override
protected void onCreate(Bundle load)
{
  super.onCreate(load);

  setContentView(R.layout.map);
  mMapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView)).getMap();

  mMapscreen = findViewById(R.id.mapScreen);
  mMapscreen.setDrawingCacheEnabled(true);
  mUnits = new UnitsI18n(this);
  mLoggerServiceManager = new GPSLoggerServiceManager(this);

  final Semaphore calulatorSemaphore = new Semaphore(0);
  Thread calulator = new Thread("OverlayCalculator")
     {
        @Override
        public void run()
        {
           Looper.prepare();
           mHandler = new Handler();
           calulatorSemaphore.release();
           Looper.loop();
        }
     };
  calulator.start();
  try
  {
     calulatorSemaphore.acquire();
  }
  catch (InterruptedException e)
  {
     Log.e(TAG, "Failed waiting for a semaphore", e);
  }
  mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

  TextView[] speeds = { (TextView) findViewById(R.id.speedview05), (TextView) findViewById(R.id.speedview04), (TextView) findViewById(R.id.speedview03), (TextView) findViewById(R.id.speedview02),
        (TextView) findViewById(R.id.speedview01), (TextView) findViewById(R.id.speedview00) };
  mSpeedtexts = speeds;
  mLastGPSSpeedView = (TextView) findViewById(R.id.currentSpeed);
  mLastGPSAltitudeView = (TextView) findViewById(R.id.currentAltitude);
  mDistanceView = (TextView) findViewById(R.id.currentDistance);

  createListeners();
  onRestoreInstanceState(load);
}

Here is the XML Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mapScreen">
 <fragment
    android:id="@+id/myMapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

Here is the logcat output:

05-22 10:35:35.081: E/AndroidRuntime(3483): FATAL EXCEPTION: main
05-22 10:35:35.081: E/AndroidRuntime(3483): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gpstracker/com.android.epicriders.Rides.RideTracking3}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.os.Looper.loop(Looper.java:137)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at java.lang.reflect.Method.invoke(Method.java:511)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at dalvik.system.NativeStart.main(Native Method)
05-22 10:35:35.081: E/AndroidRuntime(3483): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.Activity.setContentView(Activity.java:1881)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at com.polaris.epicriders.Rides.RideTracking3.onCreate(RideTracking3.java:152)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.Activity.performCreate(Activity.java:5104)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-22 10:35:35.081: E/AndroidRuntime(3483):     ... 11 more
05-22 10:35:35.081: E/AndroidRuntime(3483): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
05-22 10:35:35.081: E/AndroidRuntime(3483):     ... 21 more
05-22 10:35:35.081: E/AndroidRuntime(3483): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.android.gpstracker-1.apk
05-22 10:35:35.081: E/AndroidRuntime(3483):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-22 10:35:35.081: E/AndroidRuntime(3483):     at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
05-22 10:35:35.081: E/AndroidRuntime(3483):     ... 24 more

Am I using the SupportMapFragment wrong or not inflating it correctly? Any help would be appreciated!

Upvotes: 1

Views: 2508

Answers (1)

Bradley
Bradley

Reputation: 259

Make sure you are using the following import statement:

import com.google.android.gms.maps.SupportMapFragment;

and not:

import com.google.android.gms.maps.MapFragment;

Then make 100% your class name in the layout file is:

com.google.android.gms.maps.SupportMapFragment

By the look of your logcat message, the inflater is trying to inflate a MapFragment, not a SupportMapFragment.

Upvotes: 2

Related Questions