2016-07-26 47 views
1

我在setHint方法上得到了NPE。 logcat中顯示了這個錯誤:PlaceAutoCompleteFragment setHint NPE

void android.widget.EditText.setHint(java.lang.CharSequence)' on a null object reference. 

這裏是我的代碼:

AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
      .setTypeFilter(AutocompleteFilter.TYPE_FILTER_NONE) 
      .build(); 
    if (pickup_point == null) { 
     Fn.logD("BOOKNOW_FRAGMENT", "autocompleteFragment_null"); 
     pickup_point = (PlaceAutocompleteFragment) PlaceAutocompleteFragment.instantiate(getActivity(), "com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"); 
     getActivity().getFragmentManager().beginTransaction().replace(R.id.pickup_container, pickup_point).commit(); 
       Fn.logD("pickup_point_fragment",String.valueOf(pickup_point)); 
     pickup_point.setFilter(typeFilter); 
     if ((southwest != null)) { 
      Fn.SystemPrintLn("******haha**my curn loc is : " + southwest.longitude + " " + southwest.latitude); 
      pickup_point.setBoundsBias(new LatLngBounds(southwest, northeast)); 
     } 
     pickup_point.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       Fn.logD("BOOKNOW_FRAGMENT", "onPlaceSelected"); 
       pickuppoint_name = (String) place.getName(); 
       pickup_address = (String) place.getAddress(); 
       pickup_latlng = place.getLatLng(); 
       pickup_lat = pickup_latlng.latitude; 
       pickup_lng = pickup_latlng.longitude; 
      } 

      @Override 
      public void onError(Status status) { 
       Fn.logD("BOOKNOW_FRAGMENT", "onError"); 
       // TODO: Handle the error. 
       Fn.logD("BOOKNOW_FRAGMENT", "An error occurred: " + status); 
      } 
     }); 
     pickup_point.setHint("Pickup Point"); 
    } 
    if (dropoff_point == null) { 
     Fn.logD("BOOKNOW_FRAGMENT", "autocompleteFragment_null"); 
     dropoff_point = (PlaceAutocompleteFragment) PlaceAutocompleteFragment.instantiate(getActivity(), "com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"); 
     getActivity().getFragmentManager().beginTransaction().replace(R.id.dropoff_container, dropoff_point).commit(); 
     //pickup_point.setHint("Pickup Point"); 
     dropoff_point.setFilter(typeFilter); 
     if ((southwest != null)) { 
      dropoff_point.setBoundsBias(new LatLngBounds(southwest, northeast)); 
     } 
     dropoff_point.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       Fn.logD("BOOKNOW_FRAGMENT", "onPlaceSelected"); 
       dropoffpoint_name = (String) place.getName(); 
       dropoff_address = (String) place.getAddress(); 
      } 

這是從XML文件中的相關代碼:

<LinearLayout 
     style="@style/horizontal_LL.sm.2"> 
     <LinearLayout 
      style="@style/edittext_fragment" 
      android:id="@+id/pickup_container" 
      android:orientation="vertical"> 
     </LinearLayout> 
    </LinearLayout> 
    <LinearLayout 
     style="@style/horizontal_LL.sm.2"> 
     <LinearLayout 
      style="@style/edittext_fragment" 
      android:orientation="vertical" 
      android:id="@+id/dropoff_container"> 
     </LinearLayout> 
    </LinearLayout> 

我知道非常多關於NPE和如何解決它們,但我找不到解決方案。提前致謝。

這似乎是這個問題: https://code.google.com/p/android/issues/detail?id=23972

回答

0

之前調用setHint()方法嘗試執行未決的事務你通過調用插入PlaceAutocompleteFragment後:

getActivity().getFragmentManager().executePendingTransactions()