java - 我正在通过 Youtube 教程创建 map 搜索应用程序,但我的应用程序崩溃了

标签 java android google-maps crash

我正在尝试通过 Youtube 教程创建一个应用程序,但每当我在 SearchView 中提交位置时,我的应用程序就会崩溃。

我尝试更改 import android.support.v7.widget.SearchView;来自导入 android.widget.SearchView;

import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    private SearchView searchView;
    private SupportMapFragment mapFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        searchView = findViewById(R.id.location);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String location = searchView.getQuery().toString();
                List<Address> addressList = null;
                if (location != null || location != "") {
                    Geocoder geocoder = new Geocoder(MapsActivity.this);
                    try {
                        addressList = geocoder.getFromLocationName(location, 1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Address address = addressList.get(0);
                    LatLng latLng = new LatLng(address.getLatitude(),address.getLongitude());
                    mMap.addMarker(new MarkerOptions().position(latLng).title(location));
                    mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                }
                return false;
            }
            @Override
            public boolean onQueryTextChange(String query) {
                return false;
            }
        });
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

    }
}```

"I expect that the app would not crash when I again hit enter to submit search in SearchView."

最佳答案

改变

if (位置!= null || 位置!= "")

if (location != null || !location.equals(""))

我们无法像以前那样比较字符串。

关于java - 我正在通过 Youtube 教程创建 map 搜索应用程序,但我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56611169/

相关文章:

javascript - Google map 地标触发包含嵌入 map 的页面中的脚本的任何方式

java - 多维数组列表java

java - 如果移动了特定文件,如何读取它

java - Apache 公共(public)VFS : how to resolve file having % character in file name?

c# - 使用 layoutInflator xamarin 膨胀时出现运行时异常?

java - android 中 ScrollView 中的图像

javascript - Google Maps Api V3 - 更改 map div

javascript - 如何确定函数加载的时间?

java - 这是 Function 接口(interface)的正确用法吗?

android - 如何获取我的设备中安装了多少个应用程序以及名称或上次访问的信息?