当前位置: 首页 > 面试题库 >

Viewpager中的Google Map实施,错误无法获取提供商

万俟浩
2023-03-14
问题内容

我在ViewPager内的android Google map Integration中遇到异常,请帮助我解决此问题,我使用了以下代码,请纠正由于我做错了的错误,这是我的要求。如果我的问题不符合条件,请添加评论,我删除了我的帖子。谢谢

活动名称:

MapTradeActivity.class

package com.tradezap.toolbartest;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;


public class MapTradeActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager parent_viewpager;
    ViewPagerAdapter pageAdapter;

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

        setContentView(R.layout.activity_maptrade);

        /*toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/

        parent_viewpager = (ViewPager) findViewById(R.id.parent_viewpager);
        setupViewPager(parent_viewpager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(parent_viewpager);
        parent_viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //tabLayout.setOnTabSelectedListener(onTabSelectedListener(parent_viewpager));
        setupTabIcons();
    }


    private void setupTabIcons() {
        int[] tabIcons = {

                R.drawable.ic_settings,R.drawable.ic_category_24dp,R.drawable.ic_deal1

                /*R.drawable.ic_tab_call,
                R.drawable.ic_tab_contacts,
                R.drawable.ic_tab_call*/
        };

        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);

        /*tabLayout.getTabAt(6).setIcon(tabIcons[6]);
        tabLayout.getTabAt(7).setIcon(tabIcons[7]);*/
    }

    private void setupViewPager(ViewPager viewPager) {
        pageAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        pageAdapter.addFrag(new fragment_settings());
        pageAdapter.addFrag(new fragment_category());
        pageAdapter.addFrag(new fragment_deal());

        parent_viewpager.setAdapter(pageAdapter);

        // parent_viewpager.setOnPageChangeListener(pageChangeListener);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        //private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            System.out.println(""+mFragmentList.size());
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment) {
            mFragmentList.add(fragment);
            //mFragmentTitleList.add(title);
        }

        /*public void addFrag(Fragment fragment,String title) {
            mFragmentList.add(fragment);
            //mFragmentTitleList.add(title);
        }*/

        @Override
        public CharSequence getPageTitle(int position) {

            // return null to display only the icon 8644960886
            return null;
        }
    }

    private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager mPager) {
        return new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mPager.setCurrentItem(tab.getPosition());
                /*System.out.println("CurrentTabPos"+tab.getPosition());
                mCurrentTabpos=tab.getPosition();*/
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

                /*mPager.setCurrentItem(tab.getPosition());
                System.out.println("ReTabPos"+tab.getPosition());
                mReTabpos=tab.getPosition();*/

            }
        };
    }


}

片段名称:fragment_deal

package com.tradezap.toolbartest;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.lang.reflect.Field;

/**
 * Created by apple1 on 13/06/16.,OnMapReadyCallback
 */
public class fragment_deal extends Fragment implements View.OnClickListener {

    private View dealFragView;

    private SupportMapFragment mSupportMapFragment;

    private GoogleMap mMap;
    private static final LatLng marker_latlng = new LatLng(11.01, 76.95);

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

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

        /*mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.location_map);


        if (mSupportMapFragment == null) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mSupportMapFragment = SupportMapFragment.newInstance();
            fragmentTransaction.replace(R.id.location_map, mSupportMapFragment).commit();
        }


        if (mSupportMapFragment != null) {
            mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
                public LatLng LatLang;

                @Override
                public void onMapReady(GoogleMap googleMap) {
                    if (googleMap != null) {

                        googleMap.getUiSettings().setAllGesturesEnabled(true);

                        //-> marker_latlng // MAKE THIS WHATEVER YOU WANT

                        CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(15.0f).build();
                        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
                        googleMap.moveCamera(cameraUpdate);

                    }

                }
            });
        }*/

            return dealFragView;

        }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        mSupportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
        if (mSupportMapFragment == null) {
            mSupportMapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_container, mSupportMapFragment).commit();
        }


/***at this time google play services are not initialize so get map and add what ever you want to it in onResume() or onStart() **/
    }



    @Override
    public void onResume() {
        super.onResume();
        if (mMap == null) {
            mMap = mSupportMapFragment.getMap();
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
        }
    }
    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDetach() {
        super.onDetach();

        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

   /* @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
       // mMap.addCircle(sydney);

    }*/
}

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-library android:name="com.google.android.maps" android:required="true" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapTradeActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"></activity>
    </application>

</manifest>

面对异常日志

06-13 17:37:34.977 16326-16326/com.tradezap.googlemaptest E/AndroidRuntime: FATAL EXCEPTION: main Process: com.tradezap.googlemaptest, PID: 16326 java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.tradezap.googlemaptest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.tradezap.googlemaptest-1/lib/arm, /vendor/lib, /system/lib]]
                                                                                at android.app.ActivityThread.installProvider(ActivityThread.java:5177)
                                                                                at android.app.ActivityThread.installContentProviders(ActivityThread.java:4769)
                                                                                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4709)
                                                                                at android.app.ActivityThread.-wrap1(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                             Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.tradezap.googlemaptest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.tradezap.googlemaptest-1/lib/arm, /vendor/lib, /system/lib]]
                                                                                at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                                at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                                at android.app.ActivityThread.installProvider(ActivityThread.java:5162)
                                                                                at android.app.ActivityThread.installContentProviders(ActivityThread.java:4769) 
                                                                                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4709) 
                                                                                at android.app.ActivityThread.-wrap1(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
                                                                                Suppressed: java.lang.ClassNotFoundException: com.google.firebase.provider.FirebaseInitProvider
                                                                                at java.lang.Class.classForName(Native Method)
                                                                                at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                                at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                                at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                        ... 12 more
                                                                             Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

我已经按照这篇文章如何使用ViewPager在片段上放置Google Maps V2

请需要帮助,在此先感谢。


问题答案:

我有解决方案,而不是使用onViewCreated()方法代替onActivityCreated()方法,而是使用了构建版本目标API 23,谢谢。

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        insertNestedFragment();

    }

    // Embeds the child fragment dynamically
    private void insertNestedFragment() {
        FragmentManager fm = getChildFragmentManager();
        mSupportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.child_fragment_container);
        if (mSupportMapFragment == null) {
            mSupportMapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.child_fragment_container, mSupportMapFragment).commit();
        }
    }


 类似资料: