【问题标题】:getContext() and getActivity() with FragmentActivitygetContext() 和 getActivity() 与 FragmentActivity
【发布时间】:2017-02-05 13:29:01
【问题描述】:

我需要在 FragmentActivity 中使用 getActivity() 和 getContext() 方法。怎么做?我不能扩展 Fragment 类(我现在不能这样做)。也许我可以施法或别的什么。需要在这堂课上做。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private LatLng latLng;
    private Marker currLocationMarker;
    private GoogleMap mMap;

    @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);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        buildGoogleApiClient();
    }

    private synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    public void onConnected(@Nullable Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{
                            android.Manifest.permission.READ_EXTERNAL_STORAGE,
                            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            android.Manifest.permission.ACCESS_FINE_LOCATION

                    },
                    100);
            return;
        }
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            //place marker at current position
            //mGoogleMap.clear();
            latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Position");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            currLocationMarker = mMap.addMarker(markerOptions);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(latLng, 5);
            mMap.animateCamera(yourLocation);
        }

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000); //5 seconds
        mLocationRequest.setFastestInterval(3000); //3 seconds
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter
    }
}

【问题讨论】:

  • 您已经有一个活动,为什么还需要另一个?

标签: android android-fragments


【解决方案1】:

getActivity() 和 getContext() 是严格的 Fragment 方法。由于 FragmentActivity 类扩展了 Activity 类,因此替代方案分别是 'this' 和 'getApplicationContext()'。

例如

mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())

可以变成,简单

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())

【讨论】:

  • 我可以用 getActivity() 做什么?
  • 由于您的 FragmentActivity 本身是一个 Activity,只需使用“this”而不是 getActivity()...“this”会导致对象引用自身,从而为 Activity 提供句柄。在 Fragment 中,getActivity() 为管理 Fragment 的 Activity 提供句柄。Activity 没有其他 Activity 管理它,因此“getActivity()”不是 Activity 类中的方法
【解决方案2】:

您不能这样做,因为 FragmentActivity 是一个 Activity。所以你可以使用this 来访问你的上下文。

使用这个:

this.getResources()

而不是

getContext.getResources()

【讨论】:

  • 你能举个例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2021-12-29
  • 2014-05-10
相关资源
最近更新 更多