因此,我一直試圖從PlaceAutoComplete Fragment中獲取地點標識並將其傳遞給另一個活動。其他活動應該通過從Places.GeoDataApi.getPlaceById()方法獲取所有必要的信息來獲取地點id並擴大視圖。來自PlaceAutoCompleteAPI的數據沒有被填充到視圖中
這裏就是PlaceAutoComplete片段存儲類:
public class PlaceSearch extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_search);
//Hiding SupportActionBar
try {
getSupportActionBar().hide();
} catch (Exception e) {
e.printStackTrace();
}
//PlaceAutoComplete Search Implementation
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i(String.valueOf(this), "Place: " + place.getName() + "\nID: " + place.getId());
String placeId = place.getId();
try {
Intent intent = new Intent(PlaceSearch.this, PlaceDetailsFromSearch.class);
Bundle extras = new Bundle();
extras.putString("placeID", placeId);
intent.putExtras(extras);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(Status status) {
Log.i(String.valueOf(this), "An error occurred: " + status);
}
});
}}
而這裏就是我試圖誇大數據的類:
public class PlaceDetailsFromSearch extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
String placeId, placeName, placeAddress, placeContact;
String placeWeblink;
float placeRating;
//Views References
RatingBar ratingBar;
TextView tv_place_name, tv_address, tv_call_info, tv_website_info;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_details_search);
Intent intent = getIntent();
if (intent != null) {
Bundle extras = intent.getExtras();
placeId = extras.getString("placeId");
}
//views
tv_place_name = (TextView) findViewById(R.id.tv_place_name_pd);
tv_address = (TextView) findViewById(R.id.tv_address);
tv_call_info = (TextView) findViewById(R.id.tv_call_info);
tv_website_info = (TextView) findViewById(R.id.tv_website_info);
ratingBar = (RatingBar) findViewById(R.id.rb_rating);
//Google API Client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
loadData();
}
private void loadData() {
Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(@NonNull PlaceBuffer places) {
if (places.getStatus().isSuccess()) {
final Place myPlace = places.get(0);
Log.i(String.valueOf(PlaceDetailsFromSearch.this),
"Place found: " + myPlace.getName() + "\n Address: " + myPlace.getAddress());
placeName = (String) myPlace.getName();
tv_place_name.setText(placeName);
placeAddress = (String) myPlace.getAddress();
tv_address.setText(placeAddress);
placeContact = (String) myPlace.getPhoneNumber();
tv_call_info.setText(placeContact);
placeRating = myPlace.getRating();
placeWeblink = String.valueOf(myPlace.getWebsiteUri());
tv_website_info.setText(placeWeblink);
placeRating = myPlace.getRating();
ratingBar.setRating(placeRating);
} else {
Log.i(String.valueOf(PlaceDetailsFromSearch.this), String.valueOf(places.getStatus()));
}
places.release();
}
});
}}
而這給了我一個錯誤沒有太多有關哪個String爲空的信息。
E/UncaughtException:java.lang.IllegalArgumentException異常:給定的字符串是空的,或者在android.os.Parcel.readException(Parcel.java:1688) 在android.os.Parcel.readException(包裹空 。 java:1637) at com.google.android.gms.location.places.internal.zzj $ zza $ zza.zzb(Unknown Source) at com.google.android.gms.location.places.internal.zzh.zza (Unknown Source)
我正在失去我的頭!並且錯誤是由於case case sensitivness –
@ GautamHans有時候會發生。 :) – MMBarno
謝謝你指出。 :) –