0

Android用のコードです。現在、私が内部に持っているのは、Googleマップと、あなたの場所を確認してマップ上にマークするコードの一部だけですが、いくつかの異なるコードを試しましたが、それらのいずれも試しませんでした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 にキャストできません

MapFragmentxml で使用しSupportMapFragment、コードでキャストしようとしています。サポートライブラリを使用FragmentManagerしているため、これはさらに早く失敗します。FragmentActivity

于 2013-05-26T18:24:46.363 に答える