gpt4 book ai didi

java - Google map api 搜索位置并添加显示 NullPointEreException 错误的标记

转载 作者:行者123 更新时间:2023-12-01 18:53:23 24 4
gpt4 key购买 nike

我想使用谷歌地图搜索一个位置并在该位置上放置一个标记。输出正确显示谷歌地图,但是当我尝试在 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) {



}
}

最佳答案

onMapReady 方法中的 map 设置为 googleMap

关于java - Google map api 搜索位置并添加显示 NullPointEreException 错误的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59697903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com