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

I'm creating an Map Searching app through an Youtube tutorial but my app getting crashed

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

我尝试更改导入 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 (location != null || location != "")

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

我们不能像以前那样比较字符串。

将堆栈跟踪添加到问题中,以便更容易识别问题,但查看代码 if geocoder.getFromLocationName (location, 1) is null then 地址address = addressList.get (0)会引发NullPointerException