片段无法转换为OnMapReadyCallback

时间:2018-06-19 09:10:37

标签: android android-fragments android-mapview

我想在我的片段中添加mapview但是我在

中收到错误
  

map = mapView.getMapAsync(this);   这是
  错误:(67,36)错误:不兼容的类型:FragmentThriller无法转换为OnMapReadyCallback
  并且在   map.setMyLocationEnabled(真);其中称呼叫需要得到用户可能拒绝的许可

plz help

FragmentThriller

import android.content.Intent;
    import android.graphics.PorterDuff;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v4.content.ContextCompat;
    import android.support.v4.content.res.ResourcesCompat;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.android.msahakyan.expandablenavigationdrawer.R;
    import com.android.msahakyan.expandablenavigationdrawer.Registration;
    import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
    import com.google.android.gms.maps.CameraUpdate;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapView;
    import com.google.android.gms.maps.MapsInitializer;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.model.LatLng;

    /**
     * A simple {@link Fragment} subclass.
     * Use the {@link FragmentThriller#newInstance} factory method to
     * create an instance of this fragment.
     */
    public class FragmentThriller extends Fragment {
        MapView mapView;
        GoogleMap map;

        private static final String KEY_MOVIE_TITLE = "key_title";

        public FragmentThriller() {
            // Required empty public constructor
        }

        public static FragmentThriller newInstance(String movieTitle) {
            FragmentThriller fragmentThriller = new FragmentThriller();
            Bundle args = new Bundle();
            args.putString(KEY_MOVIE_TITLE, movieTitle);
            fragmentThriller.setArguments(args);

            return fragmentThriller;
        }

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

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState)
        {
            View v = inflater.inflate(R.layout.fragment_thriller,container,false);

            mapView = (MapView) v.findViewById( R.id.mapView );
            mapView.onCreate( savedInstanceState );

            map = mapView.getMapAsync( this );
            map.getUiSettings().setMyLocationButtonEnabled( false );
            map.setMyLocationEnabled(true);

            MapsInitializer.initialize( this.getActivity() );

            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom( new LatLng( 28.6393627,77.3783438 ),10 );
            map.animateCamera( cameraUpdate );




            ImageButton imageButton = (ImageButton)v.findViewById(R.id.movie_icon);
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity( new Intent( getActivity(), Registration.class ) );
                }
            });

            return v;
        }

        @Override
        public void onResume() {
            mapView.onResume();
            super.onResume();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mapView.onDestroy();
        }

        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mapView.onLowMemory();
        }

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

            Drawable movieIcon = ResourcesCompat.getDrawable(getResources(), R.drawable.webdesign, getContext().getTheme());
            //if (movieIcon != null) {
              //  movieIcon.setColorFilter(ContextCompat.getColor(getContext(), R.color.orange), PorterDuff.Mode.SRC_ATOP);
            //}
            ((ImageButton) view.findViewById(R.id.movie_icon)).setImageDrawable(movieIcon);

            String movieTitle = getArguments().getString(KEY_MOVIE_TITLE);
            ((TextView) view.findViewById(R.id.movie_title)).setText(movieTitle);
        }
    }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.msahakyan.expandablenavigationdrawer"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-sdk tools:overrideLibrary="in.goodiebag.carouselpicker"/>


    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/responsive"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="keyyyy"/>



        <activity android:name=".MainActivity" >
        </activity>
        <activity android:name=".Splash"
            android:label="WebTechniQ">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".fragment.digital_marketing"
            android:label="Digital Marketing">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".fragment.website_designing"
            android:label="Website Design and Development">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".fragment.DomainRegistration"
            android:label="Domain Registration">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".Registration"
            android:label="Query Form">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

    </application>

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <permission
        android:name="com.android.msahakyan.expandablenavigationdrawer.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.android.msahakyan.expandablenavigationdrawer.permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


</manifest>

1 个答案:

答案 0 :(得分:0)

你必须用你的片段实现OnMapReadyCallback。

改变你的

public class FragmentThriller extends Fragment

  public class FragmentThriller extends Fragment implements  OnMapReadyCallback