2

古いチュートリアルを新しい Google 位置情報サービスで更新しようとしています。私はGoogleチュートリアルから直接コードを使用していますが、mLocationClient = new LocationClient(this,this,this);という行があります。エラーを返します コンストラクター LocationClient(RunFragment, RunFragment, RunFragment) は未定義です

私のコードとチュートリアルの唯一の違いは、アクティビティからではなく、フラグメントの onCreateView で実行していることです。これを修正する方法はありますか?ありがとう。

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

}

}

4

2 に答える 2

2

最初の引数は、アクティビティのコンテキストでなければなりません。

MainActivity で宣言します。

public static Context c;

MainActivity の onCreate メソッドでは、次のようになります。

c = this;

これで、次のようにメソッドを呼び出すことができます:

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

注: MainActivity は、フラグメントを膨張させたアクティビティです。

于 2013-09-05T14:47:30.403 に答える
0

Fragment ではなく、Activity を取得して LocationClient に渡すだけで済みます。

mLocationClient = new LocationClient(getActivity(),this,this);
于 2014-09-05T14:14:22.803 に答える