开发者社区> 问答> 正文

Google Map API搜索位置并添加显示NullPointException错误的标记

我想使用Google地图搜索位置,然后在该位置上放置一个标记。输出正确显示google map,但是当我尝试在searchview小部件中搜索位置时,显示强制关闭。错误如下:


Process: com.example.bohon_final__001, PID: 29297
    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 com.example.bohon_final__001.Current_Location$1.onQueryTextSubmit(Current_Location.java:77)
        at android.widget.SearchView.onSubmitQuery(SearchView.java:1259)
        at android.widget.SearchView.access$1000(SearchView.java:98)
        at android.widget.SearchView$7.onEditorAction(SearchView.java:1236)
        at android.widget.TextView.onEditorAction(TextView.java:6265)
        at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是该类的XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Current_Location">


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


    <SearchView
        android:id="@+id/search_location"
        android:layout_width="401dp"
        android:layout_height="75dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/registrationedittext"
        android:elevation="5dp"
        android:iconifiedByDefault="false"
        android:queryHint="Search place"
        android:outlineAmbientShadowColor="@color/black"

        />



</RelativeLayout>

这是类代码。

package com.example.bohon_final__001;

import androidx.fragment.app.FragmentActivity;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;

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

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.SearchView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;

import java.util.List;

public class Current_Location extends FragmentActivity implements OnMapReadyCallback {
    Location currentLocation;
    FusedLocationProviderClient fusedLocationProviderClient;
    GoogleMap map;
    SupportMapFragment mapFragment;
    SearchView searchView;
    private static final int REQUEST_CODE = 101;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current__location);

        searchView=(SearchView)findViewById(R.id.search_location);
        mapFragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.Cmap);


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                String searchinitlocation=searchView.getQuery().toString();

                List<Address> addressList=null;

                if(searchinitlocation!=null && !searchinitlocation.equals(""))
                {
                    Geocoder geocoder=new Geocoder(Current_Location.this);

                    try
                    {
                        addressList=geocoder.getFromLocationName(searchinitlocation,1);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }

                    Address address=addressList.get(0);
                    LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
                    map.addMarker(new MarkerOptions().position(latLng).title(searchinitlocation));
                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
                }


                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });


        mapFragment.getMapAsync(this);


    }


    @Override
    public void onMapReady(GoogleMap googleMap) {



    }
}

展开
收起
Puppet 2020-01-14 17:21:38 665 0
1 条回答
写回答
取消 提交回答
  • 坐落map于onMapReady法googleMap。

    2020-01-14 17:21:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Boot2.0实战Redis分布式缓存 立即下载
CUDA MATH API 立即下载
API PLAYBOOK 立即下载