GoogleMap.addMarker引发null异常

时间:2019-05-07 21:16:42

标签: java android google-maps android-fragments

我试图为自定义的Google地图应用创建搜索栏,但是每次我尝试搜索位置时,该应用都会关闭。看来我正确地做了所有事情,依次讲话。问题是,当我尝试搜索位置时,它只是强制关闭应用程序,然后返回主菜单。

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;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    GoogleMap GMap;
    SupportMapFragment SMapFrag;
    SearchView searchView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        searchView = findViewById(R.id.sv_loc);
        SMapFrag = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.google_map);


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                String Loc = searchView.getQuery().toString();
                List<Address> addressList = null;

                if (Loc != null || !Loc.equals("")){
                    Geocoder geocoder = new Geocoder(MainActivity.this);
                    try {
                        addressList = geocoder.getFromLocationName(Loc, 1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Address address = addressList.get(0);
                    LatLng Lat_Lng = new LatLng(address.getLatitude(), address.getLongitude());
                    GMap.addMarker(new MarkerOptions().position(Lat_Lng).title(Loc));
                    GMap.animateCamera(CameraUpdateFactory.newLatLngZoom(Lat_Lng, 10));
                }

                return false;
            }

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

        SMapFrag.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

    }
}

Logcat:

https://drive.google.com/file/d/1uIzbJvkyefEby9Yo2vqiOQwByXl_G1uq/view?usp=sharing

我认为,如果成功,将会出现一个地图标记,并将突出显示我在搜索栏上输入的位置。会有人帮助我吗?

1 个答案:

答案 0 :(得分:0)

在您尝试将标记Sétif,36.183333,5.4 设置为空时。

这就是为什么您必须使用Servidor : SRVWEB12 ProcessID : 4748 Thread ID : 20560 StackTrace : Error at offset 71 in file:line:column <filename unknown>:0:0 . Br.Com.Cit.CafNet.Log, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null VerificarInconsistenciasConciliacao at offset 308 in file:line:column <filename unknown>:0:0 . MetLife.Morpheus.BusinessObjects.MetWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null ContinuarFaturamentoConciliacao at offset 512 in file:line:column <filename unknown>:0:0 . MetLife.Morpheus.UI.Web.Faturamento, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null ContinuarFaturamentoConciliacao at offset 1633 in file:line:column <filename unknown>:0:0 . MetLife.Morpheus.UI.Web.Faturamento, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null RaisePostBackEvent at offset 159 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ProcessRequestMain at offset 3466 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ProcessRequest at offset 177 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ProcessRequest at offset 105 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ProcessRequest at offset 379 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ProcessRequest at offset 273 in file:line:column <filename unknown>:0:0 . App_Web_zkh0keq3, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null System.Web.HttpApplication.IExecutionStep.Execute at offset 482 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ExecuteStep at offset 115 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ResumeSteps at offset 433 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Web.IHttpAsyncHandler.BeginProcessRequest at offset 157 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ProcessRequestInternal at offset 427 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ProcessRequest at offset 284 in file:line:column <filename unknown>:0:0 . System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Usuário : mlife_srviis Mensagem : MetWizardServicesImpl:VerificarInconsistenciasConciliacao Erro :: '', hexadecimal value 0x07, is an invalid character. Line 1, position 757. 事件并设置GMap变量的原因。

onMapReady

您可以了解有关GMap事件here