2017-05-03 43 views
0

我正在開發應用程序,並從對話框中的對話框佈局文件中獲取訪問片段的錯誤null。其實我想在一個對話框裏顯示一個分頁器片段內的列表視圖。在android中有分頁器片段的地圖對話框

對話框佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/map_view" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<fragment 
    android:id="@+id/mapevent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" 
    /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Close" 
    android:id="@+id/close" 
    android:background="@null" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" /> 

片段文件,在那裏我附上對話的對話。它的工作,而我試圖讓地圖對象添加標記,然後我沒有得到支持的地圖片段的對象

public class Event extends Fragment { 
private ListView listView; 
. 
. 
. 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_item_three, container, false); 
    if(map_dialog==null) 
    map_dialog= new Dialog(getActivity()); 

    listView = (ListView) view.findViewById(R.id.event_list); 
    dialog.show(); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
      dialog.show(); 

      map_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      map_dialog.setContentView(R.layout.dialog_layout); 
      map_dialog.show(); 

      MapsInitializer.initialize(getActivity()); 
      FragmentManager myFragmentManager = getActivity().getSupportFragmentManager(); 
      mMapView = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map); 
      mMapView.onCreate(map_dialog.onSaveInstanceState()); 
      mMapView.onResume();// needed to get the map to display immediately 
      mMapView.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap googleMap) { 

        latLng=new LatLng(Double.parseDouble(eventList.get(position).getLat()),Double.parseDouble(eventList.get(position).getLng())); 
        googleMap.addMarker(new MarkerOptions().position(latLng)); 
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
        dialog.dismiss(); 
       } 
      }); 
     } 
    }); 
+0

發佈崩潰日誌.... – rafsanahmad007

回答

0

試試這個:

FragmentManager myFragmentManager = getActivity().getSupportFragmentManager(); 
mMapView = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.mapevent); 

對於對話框,您可以使用MapView代替SupportMapFragment

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

爲此在您的Fragment

MapView mMapView; 


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

     dialog.show(); 

     map_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     map_dialog.setContentView(R.layout.dialog_layout); 
     map_dialog.show(); 

     mMapView = (MapView) map_dialog.findViewById(R.id.map); 
     mMapView.onCreate(map_dialog.savedInstanceState); 
     mMapView.onResume();// needed to get the map to display immediately 

     try { 
      MapsInitializer.initialize(this.getActivity()); 
     } catch (GooglePlayServicesNotAvailableException e) { 
      e.printStackTrace(); 
     } 

     mMapView.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap googleMap) { 

       latLng = new LatLng(Double.parseDouble(eventList.get(position).getLat()),Double.parseDouble(eventList.get(position).getLng())); 
       googleMap.addMarker(new MarkerOptions().position(latLng)); 
       googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
       googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
       dialog.dismiss(); 
      } 
     }); 
    } 
}); 
+1

不工作我已經檢查它,你只是改變了標識其實我忘了寫,但我使用相同的ID雙方。但感謝您的努力和努力 –

+0

嘗試使用mapview – FAT

+0

更新後的代碼在獲取谷歌地圖ID時仍爲空 –