2013-12-14 52 views
3

我試圖創建一個片段fragments.One刷卡意見應在主要活動中,直接包住GoogleMap的MapFragment.Inflating MapFragment工作ok.But通過FragmentPagerAdapter做拋出Choreographer.class沒有找到源。我無法找到一個線索,爲什麼它的基礎上this答案happens.I設置這一切了。所以我的代碼看起來是這樣的:定製的片斷裏面嵌套谷歌地圖片段

MainActivity

public class MainActivity extends FragmentActivity { 

FragPagerAdapter _fragPagerAdapter; 
ViewPager _viewPager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_layout); 

    _fragPagerAdapter = new FragPagerAdapter(getFragmentManager()); 
    _viewPager = (ViewPager)findViewById(R.id.pager); 
    _viewPager.setAdapter(_fragPagerAdapter); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 

    return true; 
} 

public class FragPagerAdapter extends FragmentPagerAdapter { 

    public FragPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 

     Fragment fragment = null; 

     if(position == 1){ 
     fragment = new LocMapFragment(); //CRASHES 
     }else{ 
      //Doesn't contain map .WORKS 
      fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
     } 

     return fragment; 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 

      return getString(R.string.title_section1).toUpperCase(l); 
     case 1: 
      return getString(R.string.title_section2).toUpperCase(l); 
     case 2: 
      return getString(R.string.title_section3).toUpperCase(l); 
     } 
     return null; 
    } 
} 


} 

LocMapFragment

public class LocMapFragment extends Fragment { 

private GoogleMap _map; 
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState){ 

    View v = inflater.inflate(R.layout.map_layout, null, false); 

    _map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
     .getMap(); 

    return v; 
    } 
} 

map_layout佈局

<?xml version="1.0" encoding="utf-8"?> 
    <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=".MainActivity" > 

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

</RelativeLayout> 

我使用在Android API的片段,而不是從支持V4.I試圖改變片段佈局類=「com.google.android.gms.maps .SupportMapFragment「仍然一樣。

UPDATE:

好了,不知怎的,它開始now.But現在情況是,如果在地圖片段是在第一刷卡片段,然後我有兩個揮筆只有text.If我滾動工作從地圖到另一個然後向後滾動,我在Choreographer.class上遇到了異常。

This answer seem to have solved the issue.

回答

1

看看這個實現,你應該爲你已經做了,在它創建地圖的片段:

public class MapFragment extends Fragment 

創建佈局: mapview.xml:

<?xml version="1.0" encoding="utf-8"?> 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
android:id="@+id/mapview" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
map:cameraZoom="12" 
map:mapType="normal" 
map:uiZoomControls="false" 
map:uiRotateGestures="true" 
map:uiScrollGestures="true" 
map:uiZoomGestures="true" 
map:uiTiltGestures="false" /> 

然後在片段中執行此操作:

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

    return inflater.inflate(R.layout.mapview, container, false); 
} 

然後拿到地圖實例:

private GoogleMap getGoogleMap() { 
     if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) { 
      SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview); 
      if (smf != null) { 
       map = smf.getMap(); 
      } 
     } 
     return map; 
    } 
+0

好了,它開始工作now.Don't得到why.But現在我有一個問題與swipes.See我的更新。 –

+0

什麼是行的代碼休息時間嗎? –

+0

不能detect.But它似乎再現落在外面查看片段,一旦這些被滾動回來。 –