我試圖為我的自定義谷歌地圖應用程序創建一個搜索欄,但每次我嘗試搜索位置時應用程序強制關閉。看起來我做的一切都是正確的,按順序說話。事情是,當我試圖搜索一個位置時,它只是強制關閉應用程序,回到主菜單。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/google_map" android:name="com.google.android.gms.maps.SupportMapFragment"/> <SearchView android:layout_width="match_parent" android:layout_height="wrap_content" android:id = "@+id/sv_loc" android:queryHint="Search for a place or location..." android:iconifiedByDefault="false" android:layout_margin="10dp" android:elevation="5dp" android:background="@drawable/bg_round" /></RelativeLayout>Java 文件:package my.maptestapplication;import android.location.Address;import android.location.Geocoder;import android.provider.Telephony;import android.support.v4.app.FragmentActivity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.SearchView;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 java.io.IOException;import java.util.List;日志貓:https://drive.google.com/file/d/1uIzbJvkyefEby9Yo2vqiOQwByXl_G1uq/view?usp=sharing我假設如果成功,會出現一個地圖標記,它會突出顯示我在搜索欄上輸入的位置。會有人幫我解決這個問題嗎?
1 回答

九州編程
TA貢獻1785條經驗 獲得超4個贊
在您嘗試將標記設置GMap為空時。
這就是為什么您必須使用onMapReady事件并設置GMap變量的原因。
@Override
public void onMapReady(GoogleMap googleMap) {
GMap = googleMap;
}
您可以在此處閱讀有關onMapReady活動的信息
添加回答
舉報
0/150
提交
取消