单击搜索按钮强制关闭

时间:2015-10-04 20:20:34

标签: android google-maps search

我在我的应用中使用谷歌地图和带搜索按钮的editText在地图上搜索某个地方。但是当我试图点击搜索按钮时,保持文本字段为空,然后应用程序显示为“强制关闭”。我没有得到需要的更改。这是我在下面给出的代码。请帮帮我。

activity_maps.xml -

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal">

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/etSearch"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="search"
        android:id="@+id/btnSearch"
        android:layout_gravity="end"
        style="?android:attr/buttonStyleSmall"
        android:onClick="onSearch" />

</LinearLayout>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

<!--The Buttons below the map fragment-->
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">

    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Book Now"
        android:id="@+id/button" />

    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Book Later"
        android:id="@+id/button2" />
</LinearLayout>

</LinearLayout>

和MapsActivity.java -

public class MapsActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

public static final String TAG = MapsActivity.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

private GoogleMap mMap;

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

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds

}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    mGoogleApiClient.connect();
}

@Override
protected void onPause() {
    super.onPause();

    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}


public void onSearch(View view) {
    EditText location_tf = (EditText) findViewById(R.id.etSearch);
    String location = location_tf.getText().toString();
    List<Address> addressList = null;

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

        Geocoder geocoder = new Geocoder(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));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(latLng.latitude, latLng.longitude), 13));


    }
}

private void setUpMapIfNeeded() {

    if(mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        if(mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(22.572646, 88.3667))
            .title("Kolkata")
            .snippet("City of Joy"));
   // mMap.setMyLocationEnabled(true);
    //mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
      //      new LatLng(22.572646, 88.3667), 13));
}

private void handleNewLocation(Location location) {
    Log.d(TAG, location.toString());

    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();

    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    /*MarkerOptions options = new MarkerOptions()
            .position(latLng)
            .title("current location");
    mMap.addMarker(options);*/
    mMap.setMyLocationEnabled(true);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
}

@Override
public void onConnected(Bundle bundle) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if (location == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
    else {
        handleNewLocation(location);
    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
/*
 * Google Play services can resolve some errors it detects.
 * If the error has a resolution, try sending an Intent to
 * start a Google Play services activity that can resolve
 * error.
 */
    if (connectionResult.hasResolution()) {
        try {
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
        /*
         * Thrown if Google Play services canceled the original
         * PendingIntent
         */
        } catch (IntentSender.SendIntentException e) {
            // Log the error
            e.printStackTrace();
        }
    } else {
    /*
     * If no resolution is available, display a dialog to the
     * user with the error.
     */
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

@Override
public void onLocationChanged(Location location) {
    handleNewLocation(location);
}

}

1 个答案:

答案 0 :(得分:0)

替换EditText location_tf = (EditText) findViewById(R.id.etSearch); 从onSearch到onCreate,在setContentView下 并改变

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

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

}
}