2013-09-05 83 views
2

我想用新的谷歌位置服務更新舊的教程。我直接從谷歌教程使用代碼,但行mLocationClient =新的LocationClient(this,this,this);返回一個錯誤構造函數LocationClient(RunFragment,RunFragment,RunFragment)未定義新的locationclient(this,this,this)編譯錯誤

我的代碼和教程的唯一區別是,我在onCreateView中運行它而不是來自Activity的片段。任何建議如何我可以糾正這一點?謝謝。

public class RunFragment extends Fragment implements 

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 

} 

}

回答

2

第一個參數必須是一個活動的上下文中。

申報您的MainActivity:

public static Context c; 

而且在MainActivity的onCreate方法:

c = this; 

現在你可以打電話給你的方法是這樣的:

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

注意: MainActivity是使您的片段膨脹的活動。

0

你只需要獲得活動,傳遞給LocationClient,而不是片段

mLocationClient = new LocationClient(getActivity(),this,this);