2

我正在尝试调用在 EditText 的 OnClickListener 中启动自动完成活动的意图。当我将 PlaceAutocompleteFragment 直接嵌入到 onCreate 中时,它工作正常。但我需要在 Click 事件的 EditText 上调用它。

这是我的代码

<?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>

和java文件

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.
            }
        }
    }
}

我收到如下错误:

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)

请帮助我。

4

1 回答 1

1

调整

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

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

this在您的代码上指的是匿名OnClickListener,因此是错误。

于 2017-12-06T10:28:12.233 回答