2016-01-20 36 views
0

我有2個佈局的一個recyclerview,
一個是我的谷歌地圖,
以及二是針對我在地圖上點了額外的細節。錯誤充氣類片段Recyclerview多視圖

我在我的適配器中的代碼是這樣的。

public class AdapterTrending extends RecyclerView.Adapter<AdapterTrending.MyTrendingHolder> { 
private int viewType = 0; 
private Context mContext; 
private ArrayList<String> reviewTrending = new ArrayList<>(); 

public AdapterTrending(Context context, ArrayList<String> reviewTrending) { 
    this.mContext = context; 
    this.reviewTrending = reviewTrending; 
} 

@Override 
public MyTrendingHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    int listViewItemType = getItemViewType(viewType); 
     if (listViewItemType == 0) { 
      return new MyGoogleMap(LayoutInflater.from(mContext).inflate(R.layout.custom_googlemap_layout, parent, false)); 
     } else { 
      return new MyTrendings(LayoutInflater.from(mContext).inflate(R.layout.custom_trending_layout, parent, false)); 
     } 
} 

@Override 
public void onBindViewHolder(MyTrendingHolder holder, int position) { 

} 

@Override 
public int getItemCount() { 
    return 10; 
} 

@Override 
public int getItemViewType(int position) { 
    if (position == 0) { 
     viewType = 0; 
    } 
    if (position >= 1) { 
     viewType = position; 
    } 
    return viewType; 
} 


class MyTrendings extends MyTrendingHolder { 
    public MyTrendings(View itemView) { 
     super(itemView); 
    } 
} 

class MyGoogleMap extends MyTrendingHolder { 
    public MyGoogleMap(View itemView) { 
     super(itemView); 
    } 
} 

class MyTrendingHolder extends RecyclerView.ViewHolder { 
    public MyTrendingHolder(View itemView) { 
     super(itemView); 
    } 
} 
} 

我有三個選項卡tabpager。
第一個標籤是這個。
第二頁和第三頁沒有任何內容。
當我刷到第三頁並回到第二頁時,
發生此錯誤。

FATAL EXCEPTION: main 
                 Process: com.toweelo, PID: 22695 
                 android.view.InflateException: Binary XML file line #17: Error inflating class fragment 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                  at com.toweelo.adapter.AdapterTrending.onCreateViewHolder(AdapterTrending.java:31) 
                  at com.toweelo.adapter.AdapterTrending.onCreateViewHolder(AdapterTrending.java:17) 
                  at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5228) 
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4453) 
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363) 
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961) 
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370) 
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333) 
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562) 
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900) 
                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102) 
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956) 
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1865) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102) 
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956) 
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1865) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102) 
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956) 
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1865) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037) 
                  at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747) 
                  at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:761) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.ViewGroup.layout(ViewGroup.java:5302) 
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102) 
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956) 
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1865) 
                  at android.view.View.layout(View.java:16542) 
                  at android.view.V 

而對於我的XML,這是錯誤指示我。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <LinearLayout 
     android:id="@+id/mapHolder" 
     android:layout_width="match_parent" 
     android:layout_height="160dp"> 

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

回答

2

我建議,膨脹SupportMapFragment 編程,而不是在XML中添加片段。

public class AdapterTrending extends RecyclerView.Adapter<AdapterTrending.MyTrendingHolder> { 

private FragmentManager mFragmentManager; 
private int viewType = 0; 
private Context mContext; 
private ArrayList<String> reviewTrending = new ArrayList<>(); 

public AdapterTrending(Context context, FragmentManager fragmentManager, ArrayList<String> reviewTrending) { 
    this.mContext = context; 
    this.mFragmentManager = fragmentManager; 
    this.reviewTrending = reviewTrending; 
} 

@Override 
public MyTrendingHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    int listViewItemType = getItemViewType(viewType); 
     if (listViewItemType == 0) { 
      return new MyGoogleMap(LayoutInflater.from(mContext).inflate(R.layout.custom_googlemap_layout, parent, false)); 
     } else { 
      return new MyTrendings(LayoutInflater.from(mContext).inflate(R.layout.custom_trending_layout, parent, false)); 
     } 
} 

    @Override 
    public void onBindViewHolder(ExperienceListHolder holder, int position) { 
    // Setup other views here 

    SupportMapFragment fragment = SupportMapFragment.newInstance(options); 
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
      fragmentTransaction 
      .replace(R.id.map_container, fragment) 
      .addToBackStack(null) 
      .commit(); 
    fragment.getMapAsync(this); 
} 

@Override 
public int getItemCount() { 
    return 10; 
} 

@Override 
public int getItemViewType(int position) { 
    if (position == 0) { 
     viewType = 0; 
    } 
    if (position >= 1) { 
     viewType = position; 
    } 
    return viewType; 
} 


class MyTrendings extends MyTrendingHolder { 
    public MyTrendings(View itemView) { 
     super(itemView); 
    } 
} 

class MyGoogleMap extends MyTrendingHolder { 
    public MyGoogleMap(View itemView) { 
     super(itemView); 
    } 
} 

class MyTrendingHolder extends RecyclerView.ViewHolder { 
    public MyTrendingHolder(View itemView) { 
     super(itemView); 
    } 
} 
} 

和XML會是這個樣子

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <LinearLayout 
     android:id="@+id/map_container" 
     android:layout_width="match_parent" 
     android:layout_height="160dp"> 

    </LinearLayout> 
</LinearLayout> 
+0

好吧,我會努力的..但爲什麼這樣發生了錯誤?以便我可以在下一次阻止它 –

+0

我希望你已經添加了Play服務版本和地圖V2 API密鑰到你的AndroidManifest.xml中 如果沒有,請參考這個:http://stackoverflow.com/questions/20768934/android- binary-xml-file-line-9-error-inflating-class-fragment –

+0

是的。我在我的清單中有。我有另一個有地圖和它正常工作的活動。 –