2

我正在开发一个简单的应用程序,它可以播放音乐并显示谷歌地图。当我尝试将地图放在主要活动中时,我得到“错误膨胀类片段”。

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

    Log.d(MainActivity.LOG_TAG, "activity started");

    gmap = ((MapFragment)getFragmentManager().findFragmentById(R.id.fragment1)).getMap();

    music = new MusicServiceWrapper(this);

}

这是布局文件的一部分

<fragment
    android:id="@+id/fragment1"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/linearLayout1"
    android:layout_alignLeft="@+id/linearLayout2" />

关于我为什么会收到此错误的任何想法?

编辑:我将包含一些来自 LogCat 的错误(第 132 行是指我给出的 xml 片段的第一行)

04-01 01:43:35.972: E/Trace(625): error opening trace file: No such file or directory (2)
04-01 01:43:36.572: D/AndroidRuntime(625): Shutting down VM
04-01 01:43:36.572: W/dalvikvm(625): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-01 01:43:36.582: E/AndroidRuntime(625): FATAL EXCEPTION: main
04-01 01:43:36.582: E/AndroidRuntime(625): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.kenpowers.gea/net.kenpowers.gea.MainActivity}: android.view.InflateException: Binary XML file line #132: Error inflating class fragment
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.os.Looper.loop(Looper.java:137)
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-01 01:43:36.582: E/AndroidRuntime(625):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 01:43:36.582: E/AndroidRuntime(625):  at java.lang.reflect.Method.invoke(Method.java:511)
04-01 01:43:36.582: E/AndroidRuntime(625):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-01 01:43:36.582: E/AndroidRuntime(625):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-01 01:43:36.582: E/AndroidRuntime(625):  at dalvik.system.NativeStart.main(Native Method)
04-01 01:43:36.582: E/AndroidRuntime(625): Caused by: android.view.InflateException: Binary XML file line #132: Error inflating class fragment
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-01 01:43:36.582: E/AndroidRuntime(625):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
4

2 回答 2

0

确保使用

import android.support.v4.app.Fragment;

并且还要确保使用片段的 Activity 扩展了 FragmentActivity 而不是常规的 Activity,

import android.support.v4.app.FragmentActivity;

获取 FragmentActivity 类。

我认为您的片段 XML 文件应该是这样的

<fragment
class="google.android.gms.maps.MapFragment"
android:id="@+id/fragment1"
android:name="com.yourapp.frag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout1"
android:layout_alignLeft="@+id/linearLayout2" />

您是否在清单中添加了 Maps API 密钥?

于 2013-04-01T05:25:14.850 回答
0

试试这个,在 XML 中,

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="com.google.android.gms.maps.SupportMapFragment"
 />

在 Java 中

public class MainActivity extends android.support.v4.app.FragmentActivity{

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

Log.d(MainActivity.LOG_TAG, "activity started");

gmap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment1)).getMap();

   }
}
于 2013-04-01T06:06:30.037 回答