我试图运行与下面youtube视频中显示的相同的代码。。。这是链接
但当我试图运行它的Map加载,但它停止时,我搜索任何查询(位置)。。。代码如下:
主活动.java
package com.example.maps;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.SearchView;
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 java.io.IOException;
import java.util.List;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
GoogleMap gmap;
SupportMapFragment supportMapFragment;
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView=findViewById(R.id.sv_location);
supportMapFragment=(SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.pickup);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
String location = searchView.getQuery().toString();
List<Address> addressList=null;
if(location != null || !location.equals("")){
Geocoder geocoder=new Geocoder(MainActivity.this);
try {
addressList=geocoder.getFromLocationName(location,1);
Address address = addressList.get(0);
LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
gmap.addMarker(new MarkerOptions().position(latLng).title(location));
gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
}catch (IOException e){
e.printStackTrace();
}
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
supportMapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pickup"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sv_location"
android:queryHint="Search.."
android:iconifiedByDefault="false"
android:layout_margin="10dp"
android:elevation="5dp"
android:background="@drawable/bg_round"/>
</RelativeLayout>
当我在中搜索任何位置时,出现以下错误 SearchView
:
I/Choreographer: Skipped 299 frames! The application may be doing too much work on its main thread.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.maps, PID: 6141
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.maps.MainActivity$1.onQueryTextSubmit(MainActivity.java:50)
at android.widget.SearchView.onSubmitQuery(SearchView.java:1259)
at android.widget.SearchView.-wrap8(Unknown Source:0)
at android.widget.SearchView$5.onEditorAction(SearchView.java:1236)
at android.widget.TextView.onEditorAction(TextView.java:5911)
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:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
2条答案
按热度按时间6pp0gazn1#
你还没有初始化
gmap
. 您可以像视频中显示的那样:rekjcdws2#
您正在使用访问gmap对象而不进行初始化。所以在onmapready初始化它
您可以使用清除Map