谷歌地图Api v2与位置错误Android

时间:2013-05-26 18:14:58

标签: android gps location google-maps-android-api-2

嘿大家我现在为Android的代码唯一的东西我在里面是一个谷歌地图和一段代码,将检查你的位置,并在地图上标记,但我尝试了几个不同的代码,而不是任何其中一个是workend ..我的手机上只有一个错误,说你的应用程序停止了工作。 (我在我的手机上运行我的应用程序,而不是在模拟器上)

这是我的主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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/dark_wood"
    tools:context=".Main" 
    >

    <Button
        android:id="@+id/bVastLocatie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:paddingBottom="20dp"
        android:text="Butt" 
        android:textColor="#FFFFFF"
        android:onClick="onClick_bVastLocatie"/>

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

</RelativeLayout>

这是我的班级代码:

package com.*myappname*.googleMaps;

import com.google.android.gms.maps.CameraUpdate;
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.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;

public class Main extends FragmentActivity {


    private GoogleMap googleMap;

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

    }

    private void setUpMapIfNeeded() {
        // TODO Auto-generated method stub
        if(googleMap == null){

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

            if(googleMap != null){
                setUpMap();
            }
        }

    }

    private void setUpMap() {
        //Enable MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Get LocationManager object from System service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        //Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        //Get Current Location
        Location myLocation = locationManager.getLastKnownLocation(provider);

        //set map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        //Get latitude of the current location
        double latitude = myLocation.getLatitude();

        //Get longitude of the current location
        double longitude = myLocation.getLongitude();

        //Create a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        //Show the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        //Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are Here!"));

    }

    @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;
    }
}

这是我的清单:

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

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

    <permission
        android:name="com.*myappname*.googleMaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.*myappname*.googleMaps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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.ACCES_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCES_FINE_LOCATION" />

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

    <application
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="*my API KEY*" />

        <!--  Start Scherm -->
        <activity
            android:name="com.*myappname*.googleMaps.StartScherm"
            android:label="@string/app_name"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Map -->
        <activity
            android:name="com.*myappname*.googleMaps.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.*myappname*.googleMaps.MAPS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的错误日志

05-26 21:09:10.009: E/AndroidRuntime(5394): FATAL EXCEPTION: main
05-26 21:09:10.009: E/AndroidRuntime(5394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.*my app name*.googleMaps/com.*my app name*.googleMaps.Main}: java.lang.IllegalArgumentException: invalid provider: null
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.ActivityThread.access$700(ActivityThread.java:151)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.os.Looper.loop(Looper.java:137)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.ActivityThread.main(ActivityThread.java:5293)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at java.lang.reflect.Method.invoke(Method.java:511)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at dalvik.system.NativeStart.main(Native Method)
05-26 21:09:10.009: E/AndroidRuntime(5394): Caused by: java.lang.IllegalArgumentException: invalid provider: null
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.location.LocationManager.checkProvider(LocationManager.java:1779)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1144)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at com.berendscheffers.googleMaps.Main.setUpMap(Main.java:62)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at com.berendscheffers.googleMaps.Main.setUpMapIfNeeded(Main.java:42)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at com.berendscheffers.googleMaps.Main.onCreate(Main.java:31)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.Activity.performCreate(Activity.java:5250)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-26 21:09:10.009: E/AndroidRuntime(5394):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
05-26 21:09:10.009: E/AndroidRuntime(5394):     ... 11 more

enter code here

我不知道出了什么问题,我希望你们能帮助我,干杯!

3 个答案:

答案 0 :(得分:2)

  

引起:java.lang.ClassCastException:com.google.android.gms.maps.MapFragment无法强制转换为android.support.v4.app.Fragment

您正尝试在xml中使用MapFragment并将其强制转换为代码中的SupportMapFragment。这甚至都失败了,因为您正在使用支持库中的FragmentManagerFragmentActivity

答案 1 :(得分:0)

您的stacktrace:由以下引起:java.lang.IllegalArgumentException:invalid provider:null

您没有提供者,这意味着您无法获得位置!

有一种获取位置的新方法...... Reto Meier在Google I / O 2013上展示了它。 请在15:55分钟内查看:http://www.youtube.com/watch?v=GcNNx2zdXN4

答案 2 :(得分:0)

我通过在我的片段属性中添加class =“com.google.android.gms.maps.SupportMapFragment”来解决我的问题。所以你的片段看起来应该是这样的

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

我希望它有所帮助。