2

EditText의 OnClickListener에서 자동 완성 활동을 시작하기위한 인 텐트를 호출하려고합니다. onCreate 안에 직접 PlaceAutocompleteFragment를 삽입하면 정상적으로 작동합니다. 하지만 클릭 이벤트에서 EditText를 호출해야합니다.EditText의 OnClickListener에 PlaceAutocomplete 인 텐트 호출

는 여기 아래로 오류를 받고 있어요 내 코드

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

그리고 자바 파일

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

수정

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을 의미합니다.

+1

정말 고마워요. 효과가 있습니다. 나는 그 개념을 가지고있다. – 55SK55

+0

@ 55SK55 환영합니다. – ThomasEdwin