TabLayout片段中的错误

时间:2016-06-16 10:59:56

标签: android google-maps-android-api-2 android-tablayout

我在使用TabLayout实现片段时遇到了一些问题。我有3个选项卡,我硬编码了一些值和googlemaps(v2)。当我运行程序时,它会显示三个带有所有编码值和googlemaps(v2)的选项卡。当我从第一次和第二次刷卡时它工作正常。但是,当我第三次刷它显示布局膨胀错误时。请帮助我..

这里我附加了堆栈跟踪的完整代码。

这是我的第一片段。

public class asset
{
    public ObjectId Id { get; set; }
    public string filename { get; set; }
    public int filesize { get; set; }
    public List<shortmetadata> metadata { get; set; }
}

public class shortmetadata
{
    public string id { get; set; }
    public string value { get; set; }
}

FirstFragment适配器:

        MongoClient client = new MongoClient();
        var db = client.GetDatabase("Test");
        var collection = db.GetCollection<asset>("assets");

        var assets = collection.AsQueryable().Where(i =>
        i.metadata.Any(m => m.value.Contains("hello")) ||
        i.metadata.Any(m => m.value.Contains("world")) 
        );

FirstFragment的XML:

public class RequestedBookingsFragment extends Fragment {
    SharedPreferences prefs;
    SharedPreferences.Editor editor_prefs;
    TextView tv;
    GoogleMap map;
    RecyclerView rb_recycler_view;
    List<PojoFromToLatLng> pojoFromToLatLngs;
    LinearLayoutManager llm;


    public RequestedBookingsFragment() {
        // Required empty public constructor
    }


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

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_requested_bookings, container, false);

        tv = (TextView) rootView.findViewById(R.id.tv_id);
        rb_recycler_view = (RecyclerView) rootView.findViewById(R.id.recyler_rb);


        llm = new LinearLayoutManager(getActivity());
        rb_recycler_view.setLayoutManager(llm);
        rb_recycler_view.setHasFixedSize(true);
        intializeData();
        initializeAdapter();
        return rootView;

    }

    private void intializeData() {
        pojoFromToLatLngs = new ArrayList<>();
        pojoFromToLatLngs.add(new PojoFromToLatLng("JayaMahal Office", "Koramangala", "22:22:22", "Accepted"));

    }

    private void initializeAdapter() {
        RecyclerCardMapAdapter adapter = new RecyclerCardMapAdapter(pojoFromToLatLngs);

        rb_recycler_view.setAdapter(adapter);
    }

这是我的第二篇片段。

public class RecyclerCardMapAdapter extends RecyclerView.Adapter<RecyclerCardMapAdapter.MapBookingsHolder> {
    GoogleMap mMap;
    public class MapBookingsHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        CardView main_card, sub_card;
        TextView from_address_text,to_address_text, time_tv, booking_status_tv;
        Button accept_booking_btn;


        public MapBookingsHolder(View itemView) {
            super(itemView);
            from_address_text = (TextView) itemView.findViewById(R.id.from_rb);
            to_address_text = (TextView) itemView.findViewById(R.id.to_rb);
            time_tv = (TextView) itemView.findViewById(R.id.time_id_rb);
            booking_status_tv = (TextView) itemView.findViewById(R.id.booking_status_id_rb);
            accept_booking_btn = (Button) itemView.findViewById(R.id.bookings_accept_button_rb);
        }

        @Override
        public void onClick(View v) {

        }


    }
    List<PojoFromToLatLng> pojoFromToLatLngs;
    RecyclerCardMapAdapter(List<PojoFromToLatLng> pojoFromToLatLngs1)
    {

        this.pojoFromToLatLngs=pojoFromToLatLngs1;
    }

    @Override
    public RecyclerCardMapAdapter.MapBookingsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.requested_bookings_card_map, parent,false);
        MapBookingsHolder mbh=new MapBookingsHolder(v);
        return mbh;
    }

    @Override
    public void onBindViewHolder(RecyclerCardMapAdapter.MapBookingsHolder holder, int position) {

    }


    @Override
    public int getItemCount() {
        return pojoFromToLatLngs.size();
    }
}

第二片段的适配器:

<FrameLayout 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="com.cabbi.appstract.RequestedBookingsFragment">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Requested Bookings"
        android:id="@+id/tv_id"
        android:textSize="20dp"/>


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recyler_rb">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>
</FrameLayout>

XML:

     public class AcceptedBookingsFragment extends Fragment {
    RecyclerView ab_recycler_view;
    List<PojoFromToLatLng> pojoFromToLatLngs;
    LinearLayoutManager llm;


    public AcceptedBookingsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
      View rootView = inflater.inflate(R.layout.fragment_accepted_bookings, container, false);
        ab_recycler_view = (RecyclerView) rootView.findViewById(R.id.recyler_ab);
        llm = new LinearLayoutManager(getActivity());
        ab_recycler_view.setLayoutManager(llm);
        ab_recycler_view.setHasFixedSize(true);
        intializeData();
        initializeAdapter();
        return rootView;
    }

    private void intializeData() {
        pojoFromToLatLngs = new ArrayList<>();
        pojoFromToLatLngs.add(new PojoFromToLatLng("JayaMahal Office", "Koramangala", "22:22:22", "Accepted"));

    }

    private void initializeAdapter() {
        AcceptingBookingAdapter adapter = new AcceptingBookingAdapter(pojoFromToLatLngs);
        //  places_type_list_recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
        ab_recycler_view.setAdapter(adapter);
    }

}

第三部分:

public class AcceptingBookingAdapter extends RecyclerView.Adapter<AcceptingBookingAdapter.MapBookingsHolder> {
    GoogleMap mMap;
    public class MapBookingsHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        CardView main_card, sub_card;
        TextView from_address_text,to_address_text, time_tv, booking_status_tv;
        Button accept_booking_btn;


        public MapBookingsHolder(View itemView) {
            super(itemView);
            from_address_text = (TextView) itemView.findViewById(R.id.from_ab);
            to_address_text = (TextView) itemView.findViewById(R.id.to_ab);
            time_tv = (TextView) itemView.findViewById(R.id.time_id_ab);
            booking_status_tv = (TextView) itemView.findViewById(R.id.booking_status_id_ab);
            accept_booking_btn = (Button) itemView.findViewById(R.id.bookings_accepted_button_ab);
        }

        @Override
        public void onClick(View v) {

        }

//        @Override
//        public void onMapReady(GoogleMap googleMap) {
//            mMap = googleMap;
//
//        }
    }
    List<PojoFromToLatLng> pojoFromToLatLngs;
    AcceptingBookingAdapter(List<PojoFromToLatLng> pojoFromToLatLngs1)
    {

        this.pojoFromToLatLngs=pojoFromToLatLngs1;
    }

    @Override
    public AcceptingBookingAdapter.MapBookingsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.accetpted_booking_cards_map, parent,false);
        MapBookingsHolder mbh=new MapBookingsHolder(v);
        return mbh;
    }

    @Override
    public void onBindViewHolder(AcceptingBookingAdapter.MapBookingsHolder holder, int position) {

    }


    @Override
    public int getItemCount() {
        return pojoFromToLatLngs.size();
    }
}

第三片段适配器:

<FrameLayout 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="com.cabbi.appstract.AcceptedBookingsFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Accpeted Bookings"
            android:id="@+id/tv_id1"
            android:textSize="20dp"/>


        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyler_ab">

        </android.support.v7.widget.RecyclerView>

    </LinearLayout>
</FrameLayout>

XML:

public class AllBookingsFragment extends Fragment {
    TextView tv;
    RecyclerView allb_recycler_view;
    List<PojoFromToLatLng> pojoFromToLatLngs;
    LinearLayoutManager llm;


    public AllBookingsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView =  inflater.inflate(R.layout.fragment_all_bookings, container, false);
        allb_recycler_view = (RecyclerView) rootView.findViewById(R.id.recyler_allb);
        llm = new LinearLayoutManager(getActivity());
        allb_recycler_view.setLayoutManager(llm);
        allb_recycler_view.setHasFixedSize(true);
        intializeData();
        initializeAdapter();
        return rootView;
    }
    private void intializeData() {
        pojoFromToLatLngs = new ArrayList<>();
        pojoFromToLatLngs.add(new PojoFromToLatLng("JayaMahal Office", "Koramangala", "22:22:22", "Accepted"));

    }

    private void initializeAdapter() {
        AllBookingsAdapter adapter = new AllBookingsAdapter(pojoFromToLatLngs);
        //  places_type_list_recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
        allb_recycler_view.setAdapter(adapter);
    }

}

我使用上面提到的AdapterClass在所有片段中扩展下面的布局。

膨胀布局是:

public class AllBookingsAdapter extends RecyclerView.Adapter<AllBookingsAdapter.MapBookingsHolder> {
    GoogleMap mMap;
    public class MapBookingsHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        CardView main_card, sub_card;
        TextView from_address_text,to_address_text, time_tv, booking_status_tv;
        //Button accept_booking_btn;


        public MapBookingsHolder(View itemView) {
            super(itemView);
            from_address_text = (TextView) itemView.findViewById(R.id.from_allb);
            to_address_text = (TextView) itemView.findViewById(R.id.to_allb);
            time_tv = (TextView) itemView.findViewById(R.id.time_id_allb);
            booking_status_tv = (TextView) itemView.findViewById(R.id.booking_status_id_allb);
            //accept_booking_btn = (Button) itemView.findViewById(R.id.bookings_accept_button_allb);
        }

        @Override
        public void onClick(View v) {

        }

//        @Override
//        public void onMapReady(GoogleMap googleMap) {
//            mMap = googleMap;
//
//        }
    }
    List<PojoFromToLatLng> pojoFromToLatLngs;
    AllBookingsAdapter(List<PojoFromToLatLng> pojoFromToLatLngs1)
    {

        this.pojoFromToLatLngs=pojoFromToLatLngs1;
    }

    @Override
    public AllBookingsAdapter.MapBookingsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.all_bookings_card_map, parent,false);
        MapBookingsHolder mbh=new MapBookingsHolder(v);
        return mbh;
    }

    @Override
    public void onBindViewHolder(AllBookingsAdapter.MapBookingsHolder holder, int position) {

    }


    @Override
    public int getItemCount() {
        return pojoFromToLatLngs.size();
    }
}

我创建了三个具有相同数据但不同ID的布局(例如:适用于Adapter1-&gt; layout1,适用于Adapter2-&gt; layout2)。但是我在这里粘贴一个布局的代码,因为它们都有相同的vaules。

我从昨天开始研究这些问题。我已经用Google搜索了答案我有答案,但无法获得解决方案。

这是我的错误跟踪:

<FrameLayout 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="com.cabbi.appstract.AllBookingsFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All Bookings"
            android:id="@+id/tv_id2"
            android:textSize="20dp"/>


        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyler_allb">

        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

</FrameLayout>

请帮帮我.. 提前致谢..............

1 个答案:

答案 0 :(得分:1)

你的mapview引起了争议,你应该使用com.google.android.gms.maps.MapView 代替xml中的片段。

在这里阅读更多内容

Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment