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

时间:2018-12-26 13:30:52

标签: android

我正在尝试将“地图视图”添加到我的android应用中,但失败的应用崩溃了 注意:我已经添加了Google Map API并将权限关联到Android清单中

我的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>

和我的片段类

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();
    }
}

日志

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) 

2 个答案:

答案 0 :(得分:1)

您知道有一个向导可以将其添加到您的应用程序吗?您可以使用Android Live模板来创建它(然后,您将自动完成大部分工作)。

尝试:File> New> Google> Google Map Activity(您需要自己转换为Fragment)。

enter image description here

答案 1 :(得分:0)

我认为,初始化GoogleMap的更正确方法是使用getMapAsync。

请注意,您的类必须实现OnMapReadyCallback

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

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

   return v;
  }

 @Override
 public void onMapReady(GoogleMap googleMap) {
 mGoogleMap = googleMap;
 mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
 mGoogleMap.addMarker(new MarkerOptions().position(/*some location*/));
 mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(/*some location*/, 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();
 }