2

Ich versuche, eine Absicht aufzurufen, die Autocomplete-Aktivität in einem OnClickListener von EditText zu starten. Wenn ich ein PlaceAutocompleteFragment direkt in ein onCreate einbette, funktioniert es gut. Aber ich muss dies EditText bei Click-Ereignis aufrufen.Aufruf von PlaceAutocomplete Intent auf OnClickListener von EditText

Hier ist mein Code

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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="com.softvision.gocartsapp.gocartz.CreateRide"> 

    <EditText 
     android:id="@+id/editText_Source" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" 
     android:hint="Source" 
     android:textSize="14sp" 
     android:inputType="none" 
     android:focusable="false"/> 

    <EditText 
     android:id="@+id/editText_Destination" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" 
     android:hint="Destination" 
     android:textSize="14sp" 
     android:inputType="none" 
     android:focusable="false"/> 
</LinearLayout> 

und Java-Datei

public class CreateRide extends AppCompatActivity { 

    private int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1; 
    private EditText editTextSource, editTextDestination; 
    private String TAG = "CreateRide"; 

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

     // Handle editTextSource Click Handler 
     editTextSource = (EditText)findViewById(R.id.editText_Source); 
     editTextSource.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(this); 
        startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 
       } catch (GooglePlayServicesRepairableException e) { 
        // TODO: Handle the error. 
       } catch (GooglePlayServicesNotAvailableException e) { 
        // TODO: Handle the error. 
       } 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       Place place = PlaceAutocomplete.getPlace(this, data); 
       Log.i(TAG, "Place: " + place.getName()); 
      } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 
       Status status = PlaceAutocomplete.getStatus(this, data); 
       // TODO: Handle the error. 
       Log.i(TAG, status.getStatusMessage()); 

      } else if (resultCode == RESULT_CANCELED) { 
       // The user canceled the operation. 
      } 
     } 
    } 
} 

Ich erhalte eine Fehlermeldung wie folgt:

Error:(33, 107) error: no suitable method found for build(<anonymous OnClickListener>) 
method zzb.build(Activity) is not applicable 
(argument mismatch; <anonymous OnClickListener> cannot be converted to Activity) 
method IntentBuilder.build(Activity) is not applicable 
(argument mismatch; <anonymous OnClickListener> cannot be converted to Activity) 

Bitte helfen Sie mir zu diesem Thema.

Antwort

1

Ändern

Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) 
    .build(this); 

zu

Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) 
    .build(CreateRide.this); // notice CrateRide.this 

this auf Ihren Code bezieht sich auf die anonyme OnClickListener, daher der Fehler.

+1

Vielen Dank. Es funktioniert. Ich habe das Konzept. – 55SK55

+0

@ 55SK55 Sie sind willkommen – ThomasEdwin

Verwandte Themen