PlaceAutoCompleteAPI中的数据未被充气到视图中

时间:2017-04-19 15:13:27

标签: android google-places-api

所以,我一直试图从PlaceAutoComplete Fragment获取地点ID并将其传递给另一个活动。另一个活动应该通过从Places.GeoDataApi.getPlaceById()方法获取所有必要信息来获取地点ID并使视图膨胀。

以下是存储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:给定String为空或null                                                                                在android.os.Parcel.readException(Parcel.java:1688)                                                                                在android.os.Parcel.readException(Parcel.java:1637)                                                                                在com.google.android.gms.location.places.internal.zzj $ zza $ zza.zzb(未知来源)                                                                                在com.google.android.gms.location.places.internal.zzh.zza(未知来源)

1 个答案:

答案 0 :(得分:2)

替换它

  

placeId = extras.getString(“placeId”);

通过

  

placeId = extras.getString(“placeID”);

PlaceDetailsFromSearch.java 活动的 onCreate()方法中

。希望你的问题能够得到解决。因为您将 placeID 作为 PLaceSearch.java 活动的额外内容传递。