在EditText的OnClickListener上调用PlaceAutocomplete Intent

时间:2017-12-06 10:25:12

标签: java android android-intent onclicklistener googleplacesautocomplete

我试图调用意图在EditText的OnClickListener中启动自动完成活动。当我直接在onCreate中嵌入PlaceAutocompleteFragment时,它工作正常。但是我需要在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)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分: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,因此错误。