Search code examples
javaandroidnullpointerexceptionnavigationbarsetcontentview

Clearing a content view


Firstly this may be a two part question. So I've started using the navigation bar in my app and changing views using this bar is causing my problem.

This is the first screen of my app

This is what happens when I select the first option out of the navigation bar menu

The first image shows the start up of my app, all fields/buttons are intractable. The second image shows what happens when I select an item from the navigation bar. The view being called only consists of the button called "SCAN", however it merely combines this new view with the previous view.

Here is the relevant code from the first view/MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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);
    }

In the same MainActivity, here is the code that deals with the navigation bar. The Nav_Frame is an FrameLayout declared in the content_main.xml.

public boolean onNavigationItemSelected(MenuItem item) 
    {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        android.app.FragmentManager FragManager = getFragmentManager();
        if (id == R.id.nav_Menu) {
            FragManager.beginTransaction().replace(R.id.Nav_Frame,new MainScreen()).commit();
        } 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;
    }

This is the MainScreen() class being called by the navigation bar

public class MainScreen extends Fragment
{
    View MyView;
    Button Scan;
    RelativeLayout RelLay;
    MainActivity MA= new MainActivity();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        RelLay=(RelativeLayout)MA.findViewById(R.id.RelativeLayout);
        RelLay.removeAllViews();
        MyView = inflater.inflate(R.layout.home_screen,container,false);
        Scan=(Button)MyView.findViewById(R.id.btnScan);
        return MyView;
    }
}

I think my issue is sitting in this class. After some googling yesterday the .removeAllViews() method seemed to be what I needed. However I'm not sure which RelativeLayout the clear, the MainActivity() one or the MainScreen() one.

Another Issue is that the RelLay lines, crash the app giving me an NullPointerException.

Thanks in advance.


Solution

  • no need to remove the view of your main activity inside the fragment just simply add your fragment it will work