使用android在google地图中显示多个位置

时间:2016-08-10 08:04:18

标签: android google-maps

当我运行项目时崩溃并出现以下错误:无法启动活动java.lang.NullPointerException:尝试调用虚拟方法'com.google.android.gms.maps.model.Marker com.google.android.gms。有关空对象引用的maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)'。 任何人都可以帮助我解决这个问题或在Android谷歌地图活动中创建多个位置的另一个想法。

                                                                                    --------- beginning of crash
08-10 12:57:13.476 3250-3250/daffodilvarsity.edu.bd.studentportal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: daffodilvarsity.edu.bd.studentportal, PID: 3250
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{daffodilvarsity.edu.bd.studentportal/daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

Logcat错误是

                                                                                --------- beginning of crash
08-10 12:57:13.476 3250-3250/daffodilvarsity.edu.bd.studentportal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: daffodilvarsity.edu.bd.studentportal, PID: 3250
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{daffodilvarsity.edu.bd.studentportal/daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
                                                                                    at daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity.plotMarkers(LocationShareMapsActivity.java:107)
                                                                                    at daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity.onCreate(LocationShareMapsActivity.java:54)
                                                                                    at android.app.Activity.performCreate(Activity.java:6237)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-10 12:57:15.409 3250-3494/daffodilvarsity.edu.bd.studentportal W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
08-10 12:57:15.410 3250-3494/daffodilvarsity.edu.bd.studentportal I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:1
08-10 12:57:15.410 3250-3494/daffodilvarsity.edu.bd.studentportal I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 1
08-10 12:57:15.433 3250-3494/daffodilvarsity.edu.bd.studentportal D/ChimeraFileApk: Primary ABI of requesting process is x86_64
08-10 12:57:15.434 3250-3494/daffodilvarsity.edu.bd.studentportal D/ChimeraFileApk: Classloading successful. Optimized code found.
08-10 12:57:15.434 3250-3494/daffodilvarsity.edu.bd.studentportal D/GoogleCertificates: com.google.android.gms.googlecertificates module is loaded
08-10 12:57:15.495 3250-3494/daffodilvarsity.edu.bd.studentportal D/GoogleCertificatesImpl: Fetched 318 Google certificates
08-10 13:15:06.095 7141-7141/daffodilvarsity.edu.bd.studentportal I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
08-10 13:15:06.096 7141-7141/daffodilvarsity.edu.bd.studentportal I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
08-10 13:15:06.108 7141-7155/daffodilvarsity.edu.bd.studentportal D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                                [ 08-10 13:15:06.112  7141: 7141 D/         ]
                                                                                HostConnection::get() New Host Connection established 0x7fcde51d17c0, tid 7141


                                                                                [ 08-10 13:15:06.181  7141: 7155 D/         ]
                                                                                HostConnection::get() New Host Connection established 0x7fcde8d90300, tid 7155
08-10 13:15:06.187 7141-7155/daffodilvarsity.edu.bd.studentportal I/OpenGLRenderer: Initialized EGL, version 1.4
08-10 13:15:07.444 7141-7141/daffodilvarsity.edu.bd.studentportal I/Choreographer: Skipped 71 frames!  The application may be doing too much work on its main thread.

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission
    android:name="com.example.mapexdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LocationShareMapsActivity"
        android:label="@string/title_activity_location_share_maps" />
    <meta-data
        android:name="com.google.android.gms.vision"
        android:value="6587000" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCBe_7AckAvLMRIryGwRSvo-lhpv-VTbSY" />
</application>
</manifest>

这是Biuld.gradle文件

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
useLibrary 'org.apache.http.legacy'

dexOptions {
    incremental true
    javaMaxHeapSize "2048M"
}

defaultConfig {
    applicationId "daffodilvarsity.edu.bd.studentportal"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

        // Enabling multidex support.
        multiDexEnabled true

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.android.gms:play-services:9.2.1'
compile 'com.google.android.gms:play-services-maps:4.3.23'
}

这是我的主要Java文件LocationShareMapsActivity.java

package daffodilvarsity.edu.bd.studentportal;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
import java.util.HashMap;

public class LocationShareMapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
private HashMap<Marker, MyMarker> mMarkersHashMap;

public LocationShareMapsActivity() {
    //empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_share_maps);

    // Initialize the HashMap for Markers and MyMarker object
    mMarkersHashMap = new HashMap<Marker, MyMarker>();

    mMyMarkersArray.add(new MyMarker("Brasil", "icon1", Double.parseDouble("-28.5971788"), Double.parseDouble("-52.7309824")));
    mMyMarkersArray.add(new MyMarker("United States", "icon2", Double.parseDouble("33.7266622"), Double.parseDouble("-87.1469829")));
    mMyMarkersArray.add(new MyMarker("Canada", "icon3", Double.parseDouble("51.8917773"), Double.parseDouble("-86.0922954")));
    mMyMarkersArray.add(new MyMarker("England", "icon4", Double.parseDouble("52.4435047"), Double.parseDouble("-3.4199249")));
    mMyMarkersArray.add(new MyMarker("España", "icon5", Double.parseDouble("41.8728262"), Double.parseDouble("-0.2375882")));
    mMyMarkersArray.add(new MyMarker("Portugal", "icon6", Double.parseDouble("40.8316649"), Double.parseDouble("-4.936009")));
    mMyMarkersArray.add(new MyMarker("Deutschland", "icon7", Double.parseDouble("51.1642292"), Double.parseDouble("10.4541194")));
    mMyMarkersArray.add(new MyMarker("Atlantic Ocean", "icondefault", Double.parseDouble("-13.1294607"), Double.parseDouble("-19.9602353")));

    setUpMap();

    plotMarkers(mMyMarkersArray);
}

private void setUpMap()  {

    if (mMap == null) {

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        // Check if we were successful in obtaining the map.

        if (mMap != null)  {
            mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()   {
                @Override
                public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
                {
                    marker.showInfoWindow();
                    return true;
                }
            });
        }
    }
        else
            Toast.makeText(getApplicationContext(), "Unable to create Maps", Toast.LENGTH_SHORT).show();

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();
}

private void plotMarkers(ArrayList<MyMarker> markers)
{
    if(markers.size() > 0)
    {
        for (MyMarker myMarker : markers)   {
            MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
            markerOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.currentlocation_icon));

            Marker currentMarker = mMap.addMarker(markerOption);
            mMarkersHashMap.put(currentMarker, myMarker);
            mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
        }
    }
}

private int manageMarkerIcon(String markerIcon) {
    if (markerIcon.equals("icon1"))
        return R.drawable.icon1;
    else if(markerIcon.equals("icon2"))
        return R.drawable.icon2;
    else if(markerIcon.equals("icon3"))
        return R.drawable.icon3;
    else if(markerIcon.equals("icon4"))
        return R.drawable.icon4;
    else if(markerIcon.equals("icon5"))
        return R.drawable.icon5;
    else if(markerIcon.equals("icon6"))
        return R.drawable.icon6;
    else if(markerIcon.equals("icon7"))
        return R.drawable.icon7;
    else
        return R.drawable.icondefault;
}

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter  {
    public MarkerInfoWindowAdapter() {
    }

    @Override
    public View getInfoWindow(Marker marker)    {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker)     {
        View v  = getLayoutInflater().inflate(R.layout.infowindow_layout, null);

        MyMarker myMarker = mMarkersHashMap.get(marker);

        ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);

        TextView markerLabel = (TextView)v.findViewById(R.id.marker_label);

        TextView anotherLabel = (TextView)v.findViewById(R.id.another_label);


           markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));

        markerLabel.setText(myMarker.getmLabel());
        anotherLabel.setText("A custom text");

        return v;
    }
 }
}

这是getter和setter的MyMaker.java文件

package daffodilvarsity.edu.bd.studentportal;
public class MyMarker{
private String mLabel;
private String mIcon;
private Double mLatitude;
private Double mLongitude;

public MyMarker(String label, String icon, Double latitude, Double longitude)  {
    this.mLabel = label;
    this.mLatitude = latitude;
    this.mLongitude = longitude;
    this.mIcon = icon;
}

public String getmLabel() {
    return mLabel;
}

public void setmLabel(String mLabel) {
    this.mLabel = mLabel;
}

public String getmIcon() {
    return mIcon;
}

public void setmIcon(String icon) {
    this.mIcon = icon;
}

public Double getmLatitude() {
    return mLatitude;
}

public void setmLatitude(Double mLatitude) {
    this.mLatitude = mLatitude;
}

public Double getmLongitude() {
    return mLongitude;
}

public void setmLongitude(Double mLongitude) {
    this.mLongitude = mLongitude;
}
}

这是用于显示地图的activity_location_share_maps.xml文件

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity" />

infowindow_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/marker_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/station_info_layout"
    android:layout_width="205dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

        <ImageView
            android:id="@+id/marker_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/marker_label"
            android:layout_marginLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold" />

</LinearLayout>

<TextView
    android:id="@+id/another_label"
    android:layout_marginLeft="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

在地图准备好后绘制标记。你正在做的是检查你的mMap是否为空,但是你正在onCreate中绘制你的标记。该地图是一个异步调用,因此当您仍在尝试在UI线程上绘制标记时,它会移动到后台线程。只需在地图准备好后绘制标记。我希望这能解决你的问题。