Android-错误膨胀类片段

时间:2013-04-01 05:18:16

标签: java android google-maps

我正在使用一个简单的应用来播放音乐并显示Google地图。当我尝试在主要活动中放置一个地图时,我得到了#34;错误膨胀类片段"。

 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)

2 个答案:

答案 0 :(得分: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密钥?

答案 1 :(得分: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();

   }
}