【问题标题】:Google map V2 with android is not working?谷歌地图 V2 与 android 不工作?
【发布时间】:2013-09-05 08:22:40
【问题描述】:

大家好,我是 android 新手,我正在尝试将 Google map V2 与我的 android 应用程序连接起来。为此,我关注tutorialthis one also,但是当我每次尝试午餐时,我的应用程序都会显示以下错误。

我的 MainActivity.java

package com.example.map;


import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.SupportMapFragment;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fragmentManager = getFragmentManager();
    MapFragment mapFragment =  (MapFragment) fragmentManager.findFragmentById(R.id.map);
    map = mapFragment.getMap();
    //map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).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)));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

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

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

}

我的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


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

</RelativeLayout>

我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.map"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

     <permission
        android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name="com.example.map.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="the key" />
    </application>

</manifest>

我的小猫

09-05 08:09:55.435: E/AndroidRuntime(490): FATAL EXCEPTION: main
09-05 08:09:55.435: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.os.Looper.loop(Looper.java:123)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.reflect.Method.invokeNative(Native Method)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.reflect.Method.invoke(Method.java:521)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 08:09:55.435: E/AndroidRuntime(490):  at dalvik.system.NativeStart.main(Native Method)
09-05 08:09:55.435: E/AndroidRuntime(490): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.Activity.setContentView(Activity.java:1647)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.example.map.MainActivity.onCreate(MainActivity.java:31)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-05 08:09:55.435: E/AndroidRuntime(490):  ... 11 more
09-05 08:09:55.435: E/AndroidRuntime(490): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.map-2.apk]
09-05 08:09:55.435: E/AndroidRuntime(490):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-05 08:09:55.435: E/AndroidRuntime(490):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
09-05 08:09:55.435: E/AndroidRuntime(490):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
09-05 08:09:55.435: E/AndroidRuntime(490):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
09-05 08:09:55.435: E/AndroidRuntime(490):  ... 20 more

我的代码有什么问题??

【问题讨论】:

标签: java android google-maps


【解决方案1】:

你的最小 sdk 是 8

android:minSdkVersion="8"

您的课程必须扩展 FragmentActivty

使用 SupportMapFragment 代替 MapFragment

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

也导入

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;

编辑:

你的包名是

package="com.example.map"

所以改成

<permission
    android:name="com.example.map.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

 <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>

【讨论】:

  • 非常感谢先生回答我的问题先生。我在我的代码中应用了以下更改,现在之前的错误已解决,但现在我得到空指针异常。我不知道为什么我的 LatLng 先生也有问题。
  • @simmant 发布堆栈跟踪并发布导致 NPE 的行>
  • 读心术:map = mapFragment.getMap() 在片段布局之前被调用,因此为 null。 This answer 可能会对您有所帮助。
  • @Raghunandan 先生这行代码第 35 行 Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) .title("Hamburg"));
  • @simmant 看起来您的地图对象为空。用一个简单的 if 条件检查它并记录一些信息。正如马丁建议的那样,检查马丁提供的链接
【解决方案2】:

使用设备而不是模拟器来运行您的应用

【讨论】:

  • 非常感谢妈妈,这段代码在设备上运行良好真的妈妈非常感谢。现在真的很开心
  • mam 但问题是为什么这段代码不能在模拟器上运行??
  • 原因是版本升级只有设备肯定会升级,但模拟器仍然只支持谷歌地图 v1。但是如果我们向 Google Developers 提出这个问题会更好,因为他们没有提供这个设施
【解决方案3】:

试试这个,希望对你有帮助

package com.example.maptest;

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

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         map = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment1))
                    .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)));

                // Move the camera instantly to hamburg with a zoom of 15.
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

【讨论】:

    【解决方案4】:
    public class MainActivity extends FragmentActivity 
    {
      static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;
    
    
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
        map = mapFragment.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)));
    
    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
    
    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }
    

    }

    更改清单文件中的行

    <permission
        android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    
    
    <uses-permission android:name="in.wptrafficanalyzer.locationingooglemapv2.permission.MAPS_RECEIVE"/>
    

    而不是标记两行使用

     <permission
        android:name="com.example.map.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    
    
    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
    

    【讨论】:

    • 非常感谢妈妈的回答,我也在我的代码中更改了上述内容,但仍然收到相同的错误妈妈。
    • 你在模拟器中运行它吗?
    • yaa mam 我在模拟器中运行它,我还在其中安装了播放服务。
    • simmant,因为模拟器不支持google map v2,所以不能在模拟器中测试。
    【解决方案5】:
    // xml
    <fragment
        android:id="@+id/maps"
        android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    
    // Activity
        FragmentManager fm = getSupportFragmentManager();
        SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.maps);
    
        map = f.getExtendedMap();
    

    【讨论】:

    • 在一些代码 sn-ps 之外多一点上下文会很好。我不认为 OP 需要 3rd 方地图扩展来解决他们的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多