2017-02-27 24 views
0

我用這個代碼來創建地圖和標記,但是當我運行它,它顯示空白地圖和錯誤錯誤: - <a href="https://developers.google.com/maps/documentation/android-api/" rel="nofollow noreferrer">https://developers.google.com/maps/documentation/android-api/</a>類MapPane是公共的,應在名爲MapPane.java

我的代碼是從文件中聲明

ContactFragment.java

public class ContactFragment extends Fragment implements OnMapReadyCallback { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content_main); 

     MapFragment mapFragment = (MapFragment) getFragmentManager() 
       .findFragmentById(R.id.mapView); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     LatLng sydney = new LatLng(-33.867, 151.206); 

     map.setMyLocationEnabled(true); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); 

     map.addMarker(new MarkerOptions() 
       .title("Sydney") 
       .snippet("The most populous city in Australia.") 
       .position(sydney)); 
    } 
} 

fragment_contact.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="layout.ContactFragment" 
    android:background="@color/colorWhite" 
    android:id="@+id/contact_wrapper"> 

    <!-- TODO: Update blank fragment layout --> 

    <com.google.android.gms.maps.MapView 
     android:layout_width="match_parent" 
     android:id="@+id/mapView" 
     android:layout_height="200dp" /> 

    <TextView 
     android:text="Address - 1526 Petchaburi Rd. Makkasan Rajdhevee Bangkok 10400 Thailand" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView6" 
     android:layout_below="@+id/mapView" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="18dp" 
     android:padding="10dp" 
     android:textSize="18sp" 
     android:textStyle="normal|bold" /> 

    <TextView 
     android:text="Tel - +66-2652-7477-80, Fax +66-2652-7777" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView7" 
     android:padding="10dp" 
     android:layout_below="@+id/textView3" 
     android:layout_alignParentStart="true" 
     android:textSize="18sp" 
     android:textStyle="normal|bold" 
     android:fontFamily="sans-serif" /> 

    <TextView 
     android:text=" EMail - [email protected]" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView3" 
     android:padding="10dp" 
     android:textSize="18sp" 
     android:layout_below="@+id/textView6" 
     android:layout_alignParentStart="true" 
     android:textStyle="normal|bold" /> 

</RelativeLayout> 

這是錯誤創建一個片段是,你必須使用onCreateView()回調定義佈局,當我 enter image description here

回答

2

+0

在這裏檢查https://developer.android.com/training/basics/fragments/creating.html –

3

使用下面的代碼。

public class ContactFragment extends Fragment implements OnMapReadyCallback { 

View rootView 

    @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.content_main); 


     rootView =inflater.inflate(R.layout.content_main, container, false); 

     MapFragment mapFragment = (MapFragment) getFragmentManager() 
        rootView.findFragmentById(R.id.mapView); 
      mapFragment.getMapAsync(this); 

     return rootView; 

     } 
} 
相關問題