简体   繁体   English

如何将地图视图添加到我的Android应用fragmentactivity

[英]How Can I Add Map view to my Android app fragmentactivity

I am trying to add Map View to my android app but failed App crashed Note: I have added google map API and associate permission into android manifests 我正在尝试将“地图视图”添加到我的android应用程序中,但是应用程序崩溃了。

MY contact_view_hd.xml 我的contact_view_hd.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_animation_list"
    android:orientation="vertical">


    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

and my fragment class 和我的片段类

public class ContactDetail extends Fragment implements OnMapReadyCallback{
    MapView mMapView;
    GoogleMap mGoogleMap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.view_contact_hd, container, false);

        mMapView = (MapView) v.findViewById(R.id.mapview);
        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this); //this is important

        return v;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        LatLng pp = new LatLng(43.1, -87.9);
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.addMarker(new MarkerOptions().position(pp));
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pp, 10));
    }

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

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

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

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

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

LOG LOG

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rupom.sbl, PID: 7373
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rupom.sbl/com.rupom.sbl.ContactDetail}: java.lang.ClassCastException: com.rupom.sbl.ContactDetail cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassCastException: com.rupom.sbl.ContactDetail cannot be cast to android.app.Activity
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Did you know that there is a Wizard for adding this to your app? 您是否知道有向导可以将其添加到您的应用程序? You can use the Android Live Templates to create this (then you will have most of the work done for you automatically). 您可以使用Android Live模板创建此模板(然后您将自动完成大部分工作)。

Try: File>New>Google>Google Map Activity (you will need to convert to Fragment yourself). 试试:File> New> Google> Google Map Activity(您需要自己转换为Fragment)。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Android片段添加到FragmentActivity中? - How do I add a Android Fragment to my FragmentActivity? 如何在Android的相机视图中添加透明地图视图? - How can i add transparent map view in camera view in android? 如何在我的Android应用程序中添加按钮,以将图像视图中的图片发布到Facebook页面? - How can I add a button in my android app that posts the picture which is in image view to a Facebook page? 如何在默认Android FragmentActivity示例的引用上动态添加片段? - how can I dynamic add fragment on the reference of default Android FragmentActivity sample? 如何向 Android 中的自定义 map 标记添加字符串? - How can I add strings to my custom map marker in Android? 如何在我的android应用中添加对android 4.4的支持 - How can I add support for android 4.4 in my android app 如何在Android的自定义键盘中添加自定义视图? - How can I add custom view to my custom keyboard in android? 如何在对话框内使用地图来映射我的FragmentActivity - How to my map FragmentActivity with Maps inside dialog 如何在eclipse中查看我的Android应用程序首选项 - how can i view my android app preferences in eclipse 如何在我的应用程序中获取android垂直网格视图? - how can I get an android vertical grid view in my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM