-2
我是新的android編程,我有這個片段從互聯網上的某個地方,它說「無法解決getActivity()」然後我嘗試使用getApplicationContext(),但自動完成沒有功能。我該怎麼做才能讓它順利運行?谷歌自動完成無法解決getActivity()
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, DirectionFinderListener {
private GoogleMap mMap;
private Button btnFindPath;
//private EditText etOrigin;
//private EditText etDestination;
private List<Marker> originMarkers = new ArrayList<>();
private List<Marker> destinationMarkers = new ArrayList<>();
private List<Polyline> polylinePaths = new ArrayList<>();
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
AutoCompleteTextView etOriginView = (AutoCompleteTextView) findViewById(R.id.etOrigin);
etOriginView.setAdapter(new PlacesAutoCompleteAdapter(getActivity(), R.layout.autocomplete_list_item));
etOriginView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get data associated with the specified position
// in the list (AdapterView)
String description = (String) parent.getItemAtPosition(position);
Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();
}
});
btnFindPath = (Button) findViewById(R.id.btnFindPath);
//etOrigin = (EditText) findViewById(R.id.etOrigin);
//etDestination = (EditText) findViewById(R.id.etDestination);
btnFindPath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequest();
}
});
}
PlacesAutoCompleteAdapter類
class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
ArrayList<String> resultList;
Context mContext;
int mResource;
PlaceAPI mPlaceAPI = new PlaceAPI();
public PlacesAutoCompleteAdapter(Context context, int resource) {
super(context, resource);
mContext = context;
mResource = resource;
}
@Override
public int getCount() {
// Last item will be the footer
return resultList.size();
}
@Override
public String getItem(int position) {
return resultList.get(position);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
resultList = mPlaceAPI.autocomplete(constraint.toString());
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
任何幫助表示讚賞,謝謝提前! 我的Google Places API Web服務告訴我,我有100%的錯誤,所以這意味着我的適配器出了問題?
這就是當你複製粘貼不理解的每一行代碼,你會得到什麼的。你已經在一個'Activity'中了,所以調用'getActivity()'沒有任何意義。怎麼樣'這個'而不是? –