问题描述
我想更新旧的辅导用新的谷歌定位服务。我直接用code从谷歌的教程,但该行mLocationClient =新LocationClient(这,这,这);返回一个错误的构造LocationClient(RunFragment,RunFragment,RunFragment)未定义
I am trying to update an old tutorial with the new google location services. I am using the code directly from google tutorial, yet the line mLocationClient = new LocationClient(this,this,this); returns an error The constructor LocationClient(RunFragment, RunFragment, RunFragment) is undefined
我的code和教程之间唯一的区别是,我在一个片段,而不是从一个活动运行它onCreateView。任何建议,我怎么能纠正呢?谢谢你。
The only difference between my code and the tutorial is that I am running it in onCreateView in a fragment instead of from an Activity. Any suggestions how I can correct this? Thanks.
public class RunFragment extends Fragment implements
LocationListener的,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
LocationListener,GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener {
private Button mStartButton, mStopButton;
private TextView mStartedTextView, mLatitudeTextView,
mLongitudeTextView, mAltitudeTextView, mDurationTextView;
// A request to connect to Location Services
private LocationRequest mLocationRequest;
// Stores the current instantiation of the location client in this object
private LocationClient mLocationClient;
// Handle to SharedPreferences for this app
SharedPreferences mPrefs;
// Handle to a SharedPreferences editor
SharedPreferences.Editor mEditor;
/*
* Note if updates have been turned on. Starts out as "false"; is set to "true" in the
* method handleRequestSuccess of LocationUpdateReceiver.
*
*/
boolean mUpdatesRequested = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_run, container, false);
mStartedTextView = (TextView)view.findViewById(R.id.run_startedTextView);
mLatitudeTextView = (TextView)view.findViewById(R.id.run_latitudeTextView);
mLongitudeTextView = (TextView)view.findViewById(R.id.run_longitudeTextView);
mAltitudeTextView = (TextView)view.findViewById(R.id.run_altitudeTextView);
mDurationTextView = (TextView)view.findViewById(R.id.run_durationTextView);
mStartButton = (Button)view.findViewById(R.id.run_startButton);
mStopButton = (Button)view.findViewById(R.id.run_stopButton);
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();
/*
* Set the update interval
*/
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
// Note that location updates are off until the user turns them on
mUpdatesRequested = false;
// Open Shared Preferences
mPrefs = getActivity().getSharedPreferences(LocationUtils.SHARED_PREFERENCES, Context.MODE_PRIVATE);
// Get an editor
mEditor = mPrefs.edit();
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
mLocationClient = new LocationClient(this,this,this);
//updateUI();
return view;
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
}
推荐答案
第一个参数必须是一个活动的情况下。
The first argument has to be the context of an Activity.
在声明的MainActivity:
Declare in your MainActivity:
public static Context c;
和在MainActivity的onCreate方法:
And in the onCreate method of the MainActivity:
c = this;
现在你可以调用你的方法是这样的:
Now you can call your method like this:
mLocationClient = new LocationClient(MainActivity.c, this, this);
请注意:MainActivity是已膨胀的片段活动
Note: MainActivity is the Activity that has inflated your fragment.
这篇关于新locationclient(这,这,这)编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!