1

我的 SupportFragment 代码出错。它在扩展 XML 时遇到问题。活动开始时它会崩溃。这是我正在使用的 Java 代码:

@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);
}

这是 XML 代码:

<?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"/>

这是 logcat 输出:

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

我是在错误地使用 SupportMapFragment 还是没有正确充气?任何帮助,将不胜感激!

4

1 回答 1

2

确保您使用以下导入语句:

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

并不是:

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

然后在布局文件中将 100% 的类名设为:

com.google.android.gms.maps.SupportMapFragment

从您的 logcat 消息来看,充气机正在尝试充气 MapFragment,而不是 SupportMapFragment。

于 2013-05-22T16:17:37.907 回答