Question Detail

Why this error raise here. How can i resolve this?

HomePage.java
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.graphics.PorterDuff;

import com.ittech.example.Adapters.HorizontalAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

public class HomePage extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
//for bottom navigation
 private static final String SELECTED_ITEM = "arg_selected_item";
    private BottomNavigationView mBottomNav;
    private int mSelectedItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        //for bottom navigation
        mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
        mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                selectFragment(item);
                return true;
            }
        });

        MenuItem selectedItem;
        if (savedInstanceState != null) {
            mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
            selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
        } else {
            selectedItem = mBottomNav.getMenu().getItem(0);
        }
        selectFragment(selectedItem);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    private void selectFragment(MenuItem item) {
        Fragment frag = null;
        // init corresponding fragment
        switch (item.getItemId()) {
            case R.id.menu_home:
                frag = new Home_Fragment();
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.container,frag);
                transaction.commit();
                break;
            case R.id.menu_notifications:
//                frag = MenuFragment.newInstance(getString(R.string.text_notifications),
//                        getColorFromRes(R.color.color_notifications));
                break;
            case R.id.menu_search:
//                frag = MenuFragment.newInstance(getString(R.string.text_search),
//                        getColorFromRes(R.color.color_search));
                break;
        }

        // update selected item
        mSelectedItem = item.getItemId();

        // uncheck the other items.
        for (int i = 0; i< mBottomNav.getMenu().size(); i++) {
            MenuItem menuItem = mBottomNav.getMenu().getItem(i);
            menuItem.setChecked(menuItem.getItemId() == item.getItemId());
        }



        if (frag != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.container, frag, frag.getTag());
            ft.commit();
        }
    }

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

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
Homepagefragment.java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ittech.example.Adapters.HorizontalAdapter;

import java.util.ArrayList;

/**
 * Created by viniti on 9/21/2017.
 */

public class Home_Fragment extends Fragment {
    private String mText;
    private int mColor;

    private View mContent;
    private TextView mTextView;
    private RecyclerView horizontal_recycler_view, horizontal_recycler_view_groups,horizontal_recycler_view_management,horizontal_recycler_view_friends;
    private ArrayList<String> horizontalList,horizontalList_groups,horizontalList_management,horizontalList_friends;
    private ArrayList<Integer> horizontalImg,horizontalImg_groups,horizontalImg_management,horizontalImg_friends;
    HorizontalAdapter horizontalAdapter,horizontalAdapter_groups,horizontalAdapter_management,horizontalAdapter_friends;

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

        View view= inflater.inflate(R.layout.home_fragment, container, false);

        //for recyclerView

        horizontal_recycler_view_management=(RecyclerView) view.findViewById(R.id.horizontal_recycler_view_management);
        horizontal_recycler_view = (RecyclerView) view.findViewById(R.id.horizontal_recycler_view_recent_activity);
        horizontal_recycler_view_groups = (RecyclerView) view.findViewById(R.id.horizontal_recycler_view_groups);
        horizontal_recycler_view_friends = (RecyclerView) view.findViewById(R.id.horizontal_recycler_view_friends);

        horizontalList_friends = new ArrayList<>();

        horizontalList_friends.add("friend");
        horizontalList_friends.add("friend 2");
        horizontalList_friends.add("friend 3");
        horizontalList_friends.add("friend 4");
        horizontalList_friends.add("friend 5");

        horizontalImg_friends = new ArrayList<>();
        horizontalImg_friends.add(R.drawable.ic_friends_icon);
        horizontalImg_friends.add(R.drawable.ic_friends_icon);
        horizontalImg_friends.add(R.drawable.ic_friends_icon);
        horizontalImg_friends.add(R.drawable.ic_friends_icon);
        horizontalImg_friends.add(R.drawable.ic_friends_icon);



        horizontalList_management = new ArrayList<>();

        horizontalList_management.add("Bill");
        horizontalList_management.add("Event");
        horizontalList_management.add("Loan");
        horizontalList_management.add("WishList");
        horizontalList_management.add("Reminder");

        horizontalImg_management = new ArrayList<>();
        horizontalImg_management.add(R.drawable.ic_billing);
        horizontalImg_management.add(R.drawable.ic_event_icon);
        horizontalImg_management.add(R.drawable.ic_loan_icon);
        horizontalImg_management.add(R.drawable.ic_wishlist_icon);
        horizontalImg_management.add(R.drawable.ic_reminder_icon);



        horizontalList = new ArrayList<>();

        horizontalList.add("activity 1");
        horizontalList.add("activity 2");
        horizontalList.add("activity 3");
        horizontalList.add("activity 4");
        horizontalList.add("activity 6");
        horizontalList.add("activity 7");
        horizontalList.add("activity 6");
        horizontalList.add("activity 6");
        horizontalList.add("activity 6");
        horizontalList.add("activity 6");

        horizontalImg = new ArrayList<>();
        horizontalImg.add(R.drawable.ic_recent_activity);
        horizontalImg.add(R.drawable.ic_friends_icon);
        horizontalImg.add(R.drawable.ic_recent_activity);
        horizontalImg.add(R.drawable.ic_friends_icon);
        horizontalImg.add(R.drawable.ic_recent_activity);
        horizontalImg.add(R.drawable.ic_friends_icon);
        horizontalImg.add(R.drawable.ic_recent_activity);
        horizontalImg.add(R.drawable.ic_recent_activity);
        horizontalImg.add(R.drawable.ic_recent_activity);
        horizontalImg.add(R.drawable.ic_recent_activity);



        horizontalList_groups = new ArrayList<>();

        horizontalList_groups.add("group 1");
        horizontalList_groups.add("group 2");
        horizontalList_groups.add("group 3");
        horizontalList_groups.add("group 4");
        horizontalList_groups.add("group 5");
        horizontalList_groups.add("group 6");
        horizontalList_groups.add("group 7");
        horizontalList_groups.add("group 7");
        horizontalList_groups.add("group 8");
        horizontalList_groups.add("group 9");

        horizontalImg_groups = new ArrayList<>();
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);
        horizontalImg_groups.add(R.drawable.ic_group_icon);

        horizontalAdapter = new HorizontalAdapter(horizontalList, horizontalImg, getActivity());
        horizontalAdapter_management = new HorizontalAdapter(horizontalList_management, horizontalImg_management, getActivity());
        horizontalAdapter_friends = new HorizontalAdapter(horizontalList_friends, horizontalImg_friends, getActivity());
        LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        LinearLayoutManager horizontalLayoutgroupsManagaer = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        LinearLayoutManager horizontalLayout_management_Managaer = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        LinearLayoutManager horizontalLayout_friends_Managaer = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
        horizontal_recycler_view.setAdapter(horizontalAdapter);
        horizontal_recycler_view_groups.setLayoutManager(horizontalLayoutgroupsManagaer);
        horizontal_recycler_view_management.setLayoutManager(horizontalLayout_management_Managaer);
        horizontal_recycler_view_friends.setLayoutManager(horizontalLayout_friends_Managaer);
        horizontal_recycler_view_groups.setAdapter( new HorizontalAdapter(horizontalList_groups, horizontalImg_groups, getActivity()));
        horizontal_recycler_view_management.setAdapter( new HorizontalAdapter(horizontalList_management, horizontalImg_management, getActivity()));
        horizontal_recycler_view_friends.setAdapter( new HorizontalAdapter(horizontalList_friends, horizontalImg_friends, getActivity()));
        return view;
    }
}


Thread Reply

viniti vaddepalli

- 1 years ago

Hi, comment this code and run. 

if (frag != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(R.id.container, frag, frag.getTag()); ft.commit();

you are calling the container again.

Anonymous

- 1 years ago

you are calling the container again.