【问题标题】:fragment xml error?片段xml错误?
【发布时间】:2017-03-09 10:51:53
【问题描述】:

当我使用这个 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:name="com.example.mahmoud.googlemap.MapFragment"
       android:id="@+id/map"/>

</RelativeLayout>

当我运行MainActivity.java at setContentView(R.layout.activity_main); Line: 时出现错误:

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
  }
}

我已经完成了这门课MapFragment

package com.example.mahmoud.googlemap;

import android.graphics.BitmapFactory;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;

/**
 * Created by mahmoud on 01/05/2016.
 */
public class MapFragment extends SupportMapFragment implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        GoogleMap.OnInfoWindowClickListener,
        GoogleMap.OnMapLongClickListener,
        GoogleMap.OnMapClickListener,
        GoogleMap.OnMarkerClickListener {

//-------------------- some Global Values -------------------------------------------//

private GoogleApiClient mGoogleApiClient;
    private Location mCurrentLocation;

    private final int[] MAP_TYPES = { GoogleMap.MAP_TYPE_SATELLITE,
            GoogleMap.MAP_TYPE_NORMAL,
            GoogleMap.MAP_TYPE_HYBRID,
            GoogleMap.MAP_TYPE_TERRAIN,
            GoogleMap.MAP_TYPE_NONE };
    private int curMapTypeIndex = 0;

    //------------End Of Values   ----------------------------------------------------------------------------//


//--------------------   onViewCreated  ------------------------------------------//
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setHasOptionsMenu(true);

        mGoogleApiClient = new GoogleApiClient.Builder( getActivity() )
                .addConnectionCallbacks( this )
                .addOnConnectionFailedListener( this )
                .addApi( LocationServices.API )
                .build();

        initListeners();
    }

    public void initListeners() {
        getMap().setOnMarkerClickListener(this);
        getMap().setOnMapLongClickListener(this);
        getMap().setOnInfoWindowClickListener( this );
        getMap().setOnMapClickListener(this);
    }

    //--------------------------------------------- Life Cycle  -----------------------------------------------//
    @Override
    public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    public void onStop() {
        super.onStop();
        if( mGoogleApiClient != null && mGoogleApiClient.isConnected() ) {
            mGoogleApiClient.disconnect();
        }
    }



    //---------------------------------------------------------------------------------------//
    @Override
    public void onConnected(Bundle bundle) {
        mCurrentLocation = LocationServices
                .FusedLocationApi
                .getLastLocation( mGoogleApiClient );

        initCamera(mCurrentLocation);
    }

    private void initCamera(Location location) {

        CameraPosition position = CameraPosition.builder()
                .target( new LatLng( location.getLatitude(),
                        location.getLongitude() ) )
                .zoom( 16f )
                .bearing( 0.0f )
                .tilt( 0.0f )
                .build();

        getMap().animateCamera( CameraUpdateFactory
                .newCameraPosition(position), null );

        getMap().setMapType( MAP_TYPES[curMapTypeIndex] );
        getMap().setTrafficEnabled( true );
        getMap().setMyLocationEnabled( true );
        getMap().getUiSettings().setZoomControlsEnabled(true);
    }
//------------------------------------------------------//
    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onInfoWindowClick(Marker marker) {

    }
//--------------------------------------- On map Click -------------------------//
    @Override
    public void onMapClick(LatLng latLng) {

        MarkerOptions options = new MarkerOptions().position( latLng );
        options.title(getAddressFromLatLng( latLng ) );

        options.icon( BitmapDescriptorFactory.defaultMarker() );
        getMap().addMarker( options );
    }
//-----------------------On Map Long Click -----------------------------------------------------------------------//
@Override
public void onMapLongClick(LatLng latLng) {
    MarkerOptions options = new MarkerOptions().position( latLng );
    options.title( getAddressFromLatLng( latLng ) );

    options.icon( BitmapDescriptorFactory.fromBitmap(
            BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher)) );

    getMap().addMarker( options );
}

    //--------------------- get AddressFromlatLng() ------------------------------//
    private String getAddressFromLatLng( LatLng latLng ) {
        Geocoder geocoder = new Geocoder( getActivity() );

        String address = "";
        try {
            address = geocoder
                    .getFromLocation( latLng.latitude, latLng.longitude, 1 )
                    .get( 0 ).getAddressLine( 0 );
        } catch (Exception e ) {
        }

        return address;
    }
//---------------------------------- On Marker Click --------------------------------------------------------------------//
@Override
public boolean onMarkerClick(Marker marker) {
    marker.showInfoWindow();
    return true;
}
//---------------------- To Draw  On Map  and draw Shapes ---------------------------------//

    private void drawCircle( LatLng location ) {
        CircleOptions options = new CircleOptions();
        options.center( location );
        //Radius in meters
        options.radius( 10 );
        options.fillColor( getResources()
                .getColor( R.color.common_action_bar_splitter ) );
        options.strokeColor( getResources()
                .getColor( R.color.common_signin_btn_light_text_focused ) );
        options.strokeWidth( 10 );
        getMap().addCircle(options);
    }
//------------------------  to draw Polygn --------------------------------------------------------------//
private void drawPolygon( LatLng startingLocation ) {
    LatLng point2 = new LatLng( startingLocation.latitude + .001,
            startingLocation.longitude );
    LatLng point3 = new LatLng( startingLocation.latitude,
            startingLocation.longitude + .001 );

    PolygonOptions options = new PolygonOptions();
    options.add(startingLocation, point2, point3);

    options.fillColor( getResources()
            .getColor(R.color.common_action_bar_splitter) );
    options.strokeColor( getResources()
            .getColor(R.color.common_signin_btn_light_text_focused) );
    options.strokeWidth( 10 );

    getMap().addPolygon(options);
}
    //--------------------------------Image On Map  --------------------------------------------//
    private void drawOverlay( LatLng location, int width, int height ) {
        GroundOverlayOptions options = new GroundOverlayOptions();
        options.position(location, width, height);

        options.image(BitmapDescriptorFactory
                .fromBitmap(BitmapFactory
                        .decodeResource(getResources(),
                                R.mipmap.ic_launcher)));
        getMap().addGroundOverlay(options);
    }


}

【问题讨论】:

  • “它给我一个错误” 什么错误?
  • 错误日志是什么?
  • 您是否在您的MainActivity 中实现了您的MapFragment

标签: java android xml android-gps


【解决方案1】:

将 xml 片段中的名称更改为此

android:name="com.google.android.gms.maps.SupportMapFragment"

您的最终片段应如下所示

<fragment
   android:id="@+id/map"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:name="com.google.android.gms.maps.SupportMapFragment"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    相关资源
    最近更新 更多