无法解析方法add(int,fragment)

时间:2016-02-17 10:29:55

标签: android android-fragments

我已经在Stack Overflow的各个地方看到过这个问题,但是没有一个答案可以解决我的问题。通常的答案似乎告诉应用程序导入android.support.v4.app.Fragment;

当我导入此方法时,它会被取消,因此我不确定是否需要它。

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

public class OpenStreetMap extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_open_street_map);
    OpenStreetMapFragment osmFragment = new OpenStreetMapFragment();
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .add(R.id.osm_map, osmFragment)
            .addToBackStack(null)
            .commit();
}

}

XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/osm_map">

    <org.osmdroid.views.MapView android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

OpenStreetMapFragment

public class OpenStreetMapFragment extends FragmentActivity implements MapEventsReceiver,  LocationListener, android.location.LocationListener, SensorEventListener {

static String mapQuestApiKey;
protected MapView map;
protected GeoPoint startPoint, destinationPoint;
protected ArrayList<GeoPoint> viaPoints;
protected Marker startMarker;
protected LocationManager mLocationManager;
protected DirectedLocationOverlay myLocationOverlay;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapQuestApiKey = "xxxxxxxxxxxxxxxxxxxxxxx";
    map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    IMapController mapController = map.getController();
    mapController.setZoom(12);


    myLocationOverlay = new DirectedLocationOverlay(this);
    map.getOverlays().add(myLocationOverlay);

    if (savedInstanceState == null) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        GeoPoint geoPoint = new GeoPoint(location);
        mapController.setCenter(geoPoint);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);

        startPoint = null;
        destinationPoint = null;
        viaPoints = new ArrayList<GeoPoint>();
    } else {
        myLocationOverlay.setLocation((GeoPoint) savedInstanceState.getParcelable("location"));
        //TODO: restore other aspects of myLocationOverlay...
        startPoint = savedInstanceState.getParcelable("start");
        destinationPoint = savedInstanceState.getParcelable("destination");
        viaPoints = savedInstanceState.getParcelableArrayList("viapoints");
    }
}


@Override public void onLocationChanged(final Location pLoc) {

    GeoPoint newLocation = new GeoPoint(pLoc);
    Log.e("Latitude", ":" + newLocation.getLatitude());
    Log.e("Longitude", ":" + newLocation.getLongitude());
    if (!myLocationOverlay.isEnabled()){
        //we get the location for the first time:
        myLocationOverlay.setEnabled(true);
        map.getController().animateTo(newLocation);

    }

    myLocationOverlay.setLocation(newLocation);
    String msg = "New Latitude: " + newLocation.getLatitude() + "New Longitude: " + newLocation.getLongitude();
    myLocationOverlay.setAccuracy((int)pLoc.getAccuracy());
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

}

}

3 个答案:

答案 0 :(得分:1)

那是因为到目前为止您还没有在代码中使用Fragment类。

如果您没有使用Fragment类,则可以删除

import android.support.v4.app.Fragment;`

答案 1 :(得分:0)

问题在于OpenStreetMapFragmentFragmentActivity而不是Fragment。您正尝试将其用作Fragment。从您的代码中,我无法理解为什么要将OpenStreetMapFragmentOpenStreetMap一起使用。在我看来,您可以直接使用OpenStreetMapFragment代替OpenStreetMap

我建议您先查看ActivityFragmentActivityFragment之间的差异。有很多问题可以提供帮助。 您可以尝试thisthis。或者浏览文档herehere。或者尝试这个tutorial

我希望它能解决你的问题。如果您仍有疑问,请随时询问。

答案 2 :(得分:0)

是的,只需添加

导入android.support.v4.app.Fragment;

在添加此内容之前,请确保应删除所有与片段相关的库。 删除后,再添加一个。 您将摆脱此错误。 希望对您有帮助。