片段中的RecyclerView错误使类<unknown>膨胀

时间:2018-06-28 13:54:24

标签: java android android-recyclerview

我在主屏幕上有一个带有几个片段的导航抽屉。 在主要(默认)片段中,我有2个recyclerviews。在个人资料片段中,我在3个标签中有3个recyclerviews。

通常,当我使用Recyclerview时,没有出现“错误膨胀”或“跳过布局”错误,但片段始终存在问题。

在正常使用中,一切正常,但是当我一遍又一遍地进入片段的内部和外部时,出现“错误膨胀类”,它指向适配器的onCreateViewHolder(视图= Layoutinflater。

导航活动

public class Page_Navigation extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

Fragment fragment;
FragmentManager fragmentManager = getSupportFragmentManager();
NavigationView navigationView;

SharedPreferences mSharedPref;
DrawerLayout drawer;
private Tracker mTracker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page__navigation);

    //
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView toolbar_head = findViewById(R.id.toolbar_head);
    ImageView toolbar_image = findViewById(R.id.toolbar_image);
    ImageView toolbar_profile = findViewById(R.id.toolbar_profile);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    //

    AnalyticsApplication application = (AnalyticsApplication) getApplication();
    mTracker = application.getDefaultTracker();
    mTracker.setScreenName("page_navigation");
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());

    //FIRST SETTINGS
    setSupportActionBar(toolbar);
    Typeface customFont = Typeface.createFromAsset(getAssets(), "Montserrat-Medium.ttf");
    toolbar_head.setTypeface(customFont);

    //Get Sessions
    mSharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String session_user_name = mSharedPref.getString("session_user_name", "");
    String session_user_photo = mSharedPref.getString("session_user_photo", "");

    //Navigation Drawer
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setItemIconTintList(null);
    //

    View headerView = navigationView.getHeaderView(0);
    TextView nav_userName = (TextView) headerView.findViewById(R.id.textView_nav_userName);
    CircleImageView imageView_navigation = (CircleImageView) headerView.findViewById(R.id.imageView_navigation);
    Glide.with(getApplicationContext()).load(session_user_photo).into(imageView_navigation);
    nav_userName.setText(session_user_name);

    headerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (drawer.isDrawerOpen(Gravity.START)) {
                drawer.closeDrawer(Gravity.START);
            }

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    showProfileFragment();
                }
            }, 300);
        }
    });


    toolbar_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawer.openDrawer(GravityCompat.START);
        }
    });

    toolbar_profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment;
            FragmentManager manager = getSupportFragmentManager();
            fragment = new Nav_Profile();
            FragmentTransaction fragmentTransaction = manager.beginTransaction();
            fragmentTransaction.replace(R.id.navContent, fragment).addToBackStack(null).commit();
        }
    });
    displayDefaultFragment();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }


    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    final int id = item.getItemId();


    drawer.closeDrawer(GravityCompat.START);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            switch (id) {
                case R.id.nav_home:
                    fragment = new Nav_Home();
                    break;

                case R.id.nav_contact:
                    fragment = new Nav_Contact();
                    break;

                case R.id.nav_articles:
                    fragment = new Nav_Article();
                    break;

                case R.id.nav_about:
                    fragment = new Nav_AboutUs();
                    break;

                case R.id.nav_suggest:
                    fragment = new Nav_Suggest();
                    break;

                case R.id.nav_share:
                    fragment = new Nav_Share();
                    break;

                case R.id.nav_rateApp:
                    fragment = new Nav_RateApp();
                    break;


            }

            fragmentManager.beginTransaction()
                    .replace(R.id.navContent, fragment)
                    .addToBackStack(null)
                    .commit();

        }
    }, 350);


    return true;
}

public void displayDefaultFragment() {
    fragment = new Nav_Home();
    fragmentManager.beginTransaction().replace(R.id.navContent, fragment).commit();

}

public void showSuggestFragment() {
    fragment = new Nav_Profile();
    fragmentManager.beginTransaction().replace(R.id.navContent, fragment).addToBackStack(null).commit();
}

public void showProfileFragment() {
    fragment = new Nav_Profile();
    fragmentManager.beginTransaction().replace(R.id.navContent, fragment).addToBackStack(null).commit();
}

public void setFontNavigation() {
    Menu m = navigationView.getMenu();
    for (int i = 0; i < m.size(); i++) {
        MenuItem mi = m.getItem(i);

        //for applying a font to subMenu ...
        SubMenu subMenu = mi.getSubMenu();
        if (subMenu != null && subMenu.size() > 0) {
            for (int j = 0; j < subMenu.size(); j++) {
                MenuItem subMenuItem = subMenu.getItem(j);
                SpannableString s = new SpannableString(subMenuItem.getTitle());
                s.setSpan(new TypefaceSpan("font/mont_regular.ttf"), 0, s.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                subMenuItem.setTitle(s);
            }
        }

    }
}
}

导航的第一个(默认)片段

public class Nav_Home extends Fragment implements View.OnClickListener{

SharedPreferences mSharedPref;

private SwipeRefreshLayout swipeRefresh_home;

private CardView item_homeTop_coupons, item_homeTop_draws, item_homeTop_event;
private LinearLayout layout_all_article, layout_all_999;
private ScrollView shimmer_home;

private List<Model_ListItem> listNewItems;
private RecyclerView recyclerView_item_home;

private List<Model_Article> articleList;
private RecyclerView recyclerView_article_home;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_nav__home, container, false);
    item_homeTop_coupons = view.findViewById(R.id.item_homeTop_coupons);
    item_homeTop_draws = view.findViewById(R.id.item_homeTop_draws);
    item_homeTop_event = view.findViewById(R.id.item_homeTop_event);
    recyclerView_item_home = view.findViewById(R.id.recyclerView_item_home);
    recyclerView_article_home = view.findViewById(R.id.recyclerView_article_home);
    layout_all_article = view.findViewById(R.id.layout_all_article);
    layout_all_999 = view.findViewById(R.id.layout_all_999);
    swipeRefresh_home = view.findViewById(R.id.swipeRefresh_home);
    shimmer_home = view.findViewById(R.id.shimmer_home);
    item_homeTop_coupons.setOnClickListener(this);
    item_homeTop_draws.setOnClickListener(this);
    item_homeTop_event.setOnClickListener(this);
    layout_all_999.setOnClickListener(this);
    layout_all_article.setOnClickListener(this);

    //first settngs
    mSharedPref = PreferenceManager.getDefaultSharedPreferences(view.getContext());
    String session_user_email = mSharedPref.getString("session_user_email","");


    swipeRefresh_home.setRefreshing(true);

    fetchItemsNew();
    fetchArticlesNew();


    return view;
}


public void openProfileFragment(){
    Fragment fragment;
    FragmentManager fragmentManager = getFragmentManager();
    fragment = new Nav_Profile();
    fragmentManager.beginTransaction().replace(R.id.navContent, fragment).addToBackStack(null).commit();
}

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.item_homeTop_coupons:
            startActivity(new Intent(getContext(), Page_Coupon.class));
            break;

        case R.id.item_homeTop_draws:
            startActivity(new Intent(getContext(), Page_Draw.class));
            break;

        case R.id.item_homeTop_event:
            startActivity(new Intent(getContext(), Page_Event.class));
            break;

        case R.id.layout_all_999:
            //999 city search activity
            startActivity(new Intent(getContext(), Page_SearchCity.class));
            break;

        case R.id.layout_all_article:
            //article fragment
            Fragment fragment;
            FragmentManager fragmentManager = getFragmentManager();
            fragment = new Nav_Article();
            fragmentManager.beginTransaction().replace(R.id.navContent, fragment).addToBackStack(null).commit();
            break;


    }
}

public void seeSession(){

    //sessions
    mSharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
    String session_user_id = mSharedPref.getString("session_user_id", "");
    String session_user_email = mSharedPref.getString("session_user_email", "");
    String session_user_name = mSharedPref.getString("session_user_name", "");
    String session_user_image = mSharedPref.getString("session_user_image", "");
    String session_user_nickname = mSharedPref.getString("session_user_nickname", "");


}

public void fetchItemsNew(){
    listNewItems = new ArrayList<>();
    API_Service api_service = Client.getRetrofitInstance().create(API_Service.class);
    Call<List<Model_ListItem>> call = api_service.fetchItemsNew();
    call.enqueue(new Callback<List<Model_ListItem>>() {
        @Override
        public void onResponse(Call<List<Model_ListItem>> call, Response<List<Model_ListItem>> response) {

            if(response.code() == 200){

                listNewItems = response.body();


                recyclerView_item_home.setHasFixedSize(true);
                LinearLayoutManager layoutManager
                        = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
                recyclerView_item_home.setLayoutManager(layoutManager);
                Adapter_HomeItem adapter_homeItem = new Adapter_HomeItem(getContext(), listNewItems);
                recyclerView_item_home.setAdapter(adapter_homeItem);
                SnapHelper helper = new LinearSnapHelper();
                helper.attachToRecyclerView(recyclerView_item_home);



            }

        }

        @Override
        public void onFailure(Call<List<Model_ListItem>> call, Throwable t) {

        }
    });

}

public void fetchArticlesNew(){
    articleList = new ArrayList<>();
    API_Service api_service = Client.getRetrofitInstance().create(API_Service.class);
    Call<List<Model_Article>> callArticle = api_service.fetchArticlesNew();
    callArticle.enqueue(new Callback<List<Model_Article>>() {
        @Override
        public void onResponse(Call<List<Model_Article>> call, Response<List<Model_Article>> response) {

            if(response.code() == 200){

                articleList = response.body();


                recyclerView_article_home.setHasFixedSize(true);
                LinearLayoutManager layoutManager
                        = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
                recyclerView_article_home.setLayoutManager(layoutManager);
                Adapter_HomeArticles adapter_homeArticles = new Adapter_HomeArticles(getContext(), articleList);
                recyclerView_article_home.setAdapter(adapter_homeArticles);
                if(swipeRefresh_home.isRefreshing()){
                    swipeRefresh_home.setRefreshing(false);
                }
                shimmer_home.setVisibility(View.VISIBLE);
                swipeRefresh_home.setEnabled(false);
            }

        }

        @Override
        public void onFailure(Call<List<Model_Article>> call, Throwable t) {

        }
    });

}
}

RecyclerView适配器

public class Adapter_HomeItem extends RecyclerView.Adapter<Adapter_HomeItem.ViewHolder> {

private Context context;
private List<Model_ListItem> homeList;
String urlExtension = URL_Extension.url_extension_images;

public Adapter_HomeItem(Context context, List<Model_ListItem> homeList) {
    this.context = context;
    this.homeList = homeList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)     {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_list, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
    Glide.with(context).load(urlExtension+homeList.get(position).getItem_image()).into(holder.item_home_image);
    holder.item_home_text.setText(homeList.get(position).getItem_name());

    holder.item_home_cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //item detail
            Intent intent = new Intent(context, Page_ItemDetail.class);
            intent.putExtra("itemClicked", ""+homeList.get(position).getItem_id());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });
}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    CardView item_home_cardView;
    RoundedImageView item_home_image;
    TextView item_home_text;

    public ViewHolder(View itemView) {
        super(itemView);

        item_home_text = itemView.findViewById(R.id.item_home_text);
        item_home_image = itemView.findViewById(R.id.item_home_image);
        item_home_cardView = itemView.findViewById(R.id.item_home_cardView);
    }
}

}

错误在这里;

public Adapter_HomeArticles.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_article, null);
    int width = parent.getMeasuredWidth()/3;
    view.setLayoutParams(new RecyclerView.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
    return new ViewHolder(view);
}

投掷;

  06-28 17:20:08.543 23120-23120/? E/Zygote: v2
06-28 17:20:08.543 23120-23120/? E/Zygote: accessInfo : 0
06-28 17:20:10.468 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
06-28 17:20:10.473 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
06-28 17:23:05.018 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:10.423 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:11.108 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:11.628 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:16.288 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:17.533 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:19.008 23120-23120/turkiyedeyapilacak999sey.android E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@7c57e67
06-28 17:23:19.013 23120-23120/turkiyedeyapilacak999sey.android E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@6446414
    motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@6446414
06-28 17:23:20.643 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:22.738 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:24.468 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:27.818 23120-23120/turkiyedeyapilacak999sey.android E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
06-28 17:23:28.118 23120-23120/turkiyedeyapilacak999sey.android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: turkiyedeyapilacak999sey.android, PID: 23120
    android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class <unknown>
        at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
        at turkiyedeyapilacak999sey.android.Adapter.Adapter_HomeArticles.onCreateViewHolder(Adapter_HomeArticles.java:39)
        at turkiyedeyapilacak999sey.android.Adapter.Adapter_HomeArticles.onCreateViewHolder(Adapter_HomeArticles.java:25)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.support.v7.widget.CardView.onMeasure(CardView.java:258)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:20151)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2104)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.widget.ScrollView.onMeasure(ScrollView.java:510)
        at android.view.View.measure(View.java:20151)
        at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:622)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:20151)
        at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1210)
        at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1550)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)

1 个答案:

答案 0 :(得分:0)

尝试用null代替false代替root:

    @NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_list, null);
    return new ViewHolder(view);
}