2015-01-05 31 views
0

我已經在我的Android應用程序中實施了谷歌地圖,它是第一次正常工作。但是當我第二次嘗試進入它時,應用程序崩潰。Android - android.view.InflateException錯誤當去谷歌地圖第二次

我發現我必須在xml中以編程方式聲明supportFragment而沒有doind它。 但堆棧溢出解決方案不適用於我,因爲我正在訪問我的谷歌地圖的類擴展到PagerAdapter。它不支持getChildFragmentManager()。我如何克服這個問題?

我班

public class ViewPageAdapter extends PagerAdapter { 

    private Fragment fragmentActivity; 

    public ViewPageAdapter(ArrayList<BaseElement> item, int page, 
       Activity activity, Fragment fragmentActivity) { 
      super(); 
      this.item = item; 
      this.page = page; 
      this.activity = activity; 
      this.fragmentActivity = fragmentActivity; 
      inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      this.imageLoader = new ImageLoader(activity); 
     } 

    @Override 
     public Object instantiateItem(ViewGroup container, int position) { 

    View view = null; 


if (page == Element.THEATER_DETAIL.getType()) { 


    GoogleMap googleMap = null; 

       SupportMapFragment fm = (SupportMapFragment) fragmentActivity 
         .getFragmentManager().findFragmentById(R.id.map); 


       googleMap = fm.getMap(); 



       googleMap.addMarker(new MarkerOptions().position(
         new LatLng(7.421226f, 80.401264f)).title("Hello world")); 

       final LatLng SRILANKA = new LatLng(7.421226,80.401264); 

       CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(SRILANKA)  // Sets the center of the map to SRILANKA 
       .zoom(10)     // Sets the zoom 
       .bearing(360)    // Sets the orientation of the camera to east 
       .tilt(30)     // Sets the tilt of the camera to 30 degrees 
       .build();     // Creates a CameraPosition from the builder 
       googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

      } 

XML

<fragment 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:name="com.google.android.gms.maps.SupportMapFragment"/> 

logcat的

01-05 17:00:13.276: E/AndroidRuntime(16311): FATAL EXCEPTION: main 
01-05 17:00:13.276: E/AndroidRuntime(16311): android.view.InflateException: Binary XML file line #11: Error inflating class fragment 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at com.fortuna.cinemalk.adapter.ViewPageAdapter.instantiateItem(ViewPageAdapter.java:131) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:829) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.support.v4.view.ViewPager.populate(ViewPager.java:979) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.support.v4.view.ViewPager.populate(ViewPager.java:911) 
01-05 17:00:13.276: E/AndroidRuntime(16311): at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440) 

回答

0

你加看法?你應該在instantiateItem函數中添加視圖,

 LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.your_layout_name, null); 

然後繼續你的代碼。

+0

什麼「行爲」在這裏? – CraZyDroiD

+0

這就是你的活動..只是編輯 – chain

+0

沒有工作:( – CraZyDroiD