【问题标题】:Cannot Resolve Method add(int, fragment)无法解决方法 add(int, fragment)
【发布时间】:2016-02-17 10:29:55
【问题描述】:

我在 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();

}

}

【问题讨论】:

  • 也发布OpenStreetMapFragment的代码
  • @Rehan 我已经用 OpenStreetMapFragment 代码更新了这个问题。非常感谢

标签: android android-fragments


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    我建议您先看看ActivityFragmentActivityFragment 之间的区别。有很多问题可能会有所帮助。 你可以试试thisthis。或者通过文档herehere。或者试试这个tutorial

    希望它能解决您的问题。如果您仍有疑问,请随时在此处提问。

    【讨论】:

    • 非常感谢 Rehan!我直接使用 OpenStreetMapFragment,现在我收到以下错误。我不知道这是怎么发生的,因为我没有扩展任何片段。这里有很多解决方案,但没有一个适用于我的项目 -------------- 无法启动活动 ComponentInfo{alan.rochford.mydit.ie.walkthisway/alan.rochford. mydit.ie.walkthisway.OpenStreetMap}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    • @AlanRochford 那是因为您的 XML 中的问题。请根据您当前的代码更新您的问题,以便我知道问题出在哪里!
    • 我已经设法修复它 Rehan。我的主要活动中有一个小问题。感谢您的帮助!
    【解决方案3】:

    是的,只需添加这个

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

    在添加之前,请确保所有与片段相关的库都应该被删除。 一旦你删除然后添加这个。 你将摆脱这个错误。 希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 1970-01-01
      • 2016-09-24
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 2015-06-04
      相关资源
      最近更新 更多