Google map v2应用程序显示错误

时间:2014-06-04 11:04:39

标签: android google-maps

我从Google开发人员教程中逐步编写了谷歌地图程序。 但是,当我在智能手机上运行或avd时,它会崩溃。当我评论setContent(r.activity_main)该应用运行并显示空白页面但处于正常状态且setContent应用时,它会崩溃!

我的日志猫:

06-05 10:59:10.028: E/AndroidRuntime(387): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.please/com.example.please.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
06-05 10:59:10.028: E/AndroidRuntime(387):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-05 10:59:10.028: E/AndroidRuntime(387):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-05 10:59:10.028: E/AndroidRuntime(387):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-05 10:59:10.028: E/AndroidRuntime(387):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-05 10:59:10.028: E/AndroidRuntime(387):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 10:59:10.028: E/AndroidRuntime(387):  at android.os.Looper.loop(Looper.java:130)
06-05 10:59:10.028: E/AndroidRuntime(387):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-05 10:59:10.028: E/AndroidRuntime(387):  at java.lang.reflect.Method.invokeNative(Native Method)
06-05 10:59:10.028: E/AndroidRuntime(387):  at java.lang.reflect.Method.invoke(Method.java:507)
06-05 10:59:10.028: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 10:59:10.028: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

我的XML文件:

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

我的java文件:

package com.example.please;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

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

2 个答案:

答案 0 :(得分:0)

无需使用FragmentActivity。只需在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" >
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

在地图活动中添加以下代码:

public class MainActivity extends Activity {

    private GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {

            initilizeMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
 }

答案 1 :(得分:0)

更改此

public class MainActivity extends Activity {

public class MainActivity extends FragmentActivity {

FragmentActivity是基于支持的片段的基类。因此,如果您使用SupportMaoFragment,则您的活动必须延长FragmentActivity

另外import android.support.v4.app.FragmentActivity

您还需要将布局内容设置为活动

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