0

嘿,伙计们,我现在只有 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

不知道是哪里出了问题,希望大家帮帮我,干杯!

4

3 回答 3

2

原因:java.lang.ClassCastException: com.google.android.gms.maps.MapFragment 无法转换为 android.support.v4.app.Fragment

您正在尝试MapFragment在 xml 中使用并将其转换SupportMapFragment为代码。这甚至更早地失败了,因为您正在使用FragmentManagerFragmentActivity来自支持库。

于 2013-05-26T18:24:46.363 回答
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"
    />

我希望它有所帮助。

于 2013-08-29T02:22:57.760 回答
0

您的堆栈跟踪:引起:java.lang.IllegalArgumentException:无效提供者:null

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

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

于 2013-05-26T21:57:29.990 回答