在xml中填充类碎片问题时出错

时间:2013-12-11 21:34:33

标签: java android xml android-fragments

我想将一个地图添加到Fragment(错误,FragmentActivity?)。 java文件没有错误,而是在XML文件行65中出现运行时错误。

这是我的代码和错误(在XML中,xml也附加了)。     ....

static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                       Bundle savedInstanceState) {

  View v = inflater.inflate(R.layout.activity_map_, null, false);

  map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2)).getMap();

Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
          .title("Hamburg"));
      Marker kiel = map.addMarker(new MarkerOptions()
          .position(KIEL)
          .title("Kiel")
          .snippet("Kiel is cool")
          .icon(BitmapDescriptorFactory
              .fromResource(R.drawable.ic_launcher)));

      map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

      // Zoom in, animating the camera.
      map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  return v;
}   ...

那里没有错误。

12-11 16:19:53.590: D/AndroidRuntime(12278): Shutting down VM
12-11 16:19:53.590: W/dalvikvm(12278): threadid=1: thread exiting with uncaught exception (group=0x4141c930)
12-11 16:19:53.600: E/AndroidRuntime(12278): FATAL EXCEPTION: main
12-11 16:19:53.600: E/AndroidRuntime(12278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.soconnection/com.example.soconnection.FullscreenActivity}: android.view.InflateException: Binary XML file line #65: Error inflating class fragment
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.os.Looper.loop(Looper.java:137)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at java.lang.reflect.Method.invoke(Method.java:511)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at dalvik.system.NativeStart.main(Native Method)
12-11 16:19:53.600: E/AndroidRuntime(12278): Caused by: android.view.InflateException: Binary XML file line #65: Error inflating class fragment
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.Activity.setContentView(Activity.java:1881)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at com.example.soconnection.FullscreenActivity.onCreate(FullscreenActivity.java:23)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.Activity.performCreate(Activity.java:5104)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-11 16:19:53.600: E/AndroidRuntime(12278):    ... 11 more
12-11 16:19:53.600: E/AndroidRuntime(12278): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportsMapFragment: make sure class name exists, is public, and has an empty constructor that is public
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.Fragment.instantiate(Fragment.java:592)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.Fragment.instantiate(Fragment.java:560)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.Activity.onCreateView(Activity.java:4709)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
12-11 16:19:53.600: E/AndroidRuntime(12278):    ... 25 more
12-11 16:19:53.600: E/AndroidRuntime(12278): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportsMapFragment" on path: /data/app/com.example.soconnection-1.apk
12-11 16:19:53.600: E/AndroidRuntime(12278):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-11 16:19:53.600: E/AndroidRuntime(12278):    at android.app.Fragment.instantiate(Fragment.java:582)
12-11 16:19:53.600: E/AndroidRuntime(12278):    ... 28 more

有问题的XML专栏:

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
             65       <fragment THIS IS THE LINE. 
                        android:id="@+id/fragment2"
                        android:name="com.example.soconnection.MapActivity2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" 
                        class ="com.google.android.gms.maps.SupportsMapFragment"/>
            </LinearLayout>

我该如何解决这个问题。除非我忽略了一些事情,否则我已经将所有内容都改为getSUpportFragment等。

1 个答案:

答案 0 :(得分:0)

java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportsMapFragment"

您使用的是Android Studio吗?您可能需要进行完全重建。构建 - &gt;重建项目