gpt4 book ai didi

android - new locationclient(this,this,this) 编译错误

转载 作者:行者123 更新时间:2023-11-29 00:27:02 25 4
gpt4 key购买 nike

我正在尝试使用新的谷歌位置服务更新旧教程。我直接使用来自谷歌教程的代码,但是行 mLocationClient = new LocationClient(this,this,this);返回错误构造函数 LocationClient(RunFragment, RunFragment, RunFragment) 未定义

我的代码和教程之间的唯一区别是我在 fragment 中的 onCreateView 中运行它,而不是从 Activity 中运行。有什么建议我可以纠正这个吗?谢谢。

public class RunFragment extends Fragment implements

位置监听器,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

}

最佳答案

第一个参数必须是 Activity 的上下文。

在您的 MainActivity 中声明:

public static Context c;

在 MainActivity 的 onCreate 方法中:

c = this;

现在你可以这样调用你的方法了:

mLocationClient = new LocationClient(MainActivity.c, this, this); 

注意:MainActivity 是使您的 fragment 膨胀的 Activity。

关于android - new locationclient(this,this,this) 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com