**Logcat at com.example.appkhushveehoreca.MainActivity.setFragment(MainActivity.java:174) at com.example.appkhushveehoreca.MainActivity.onCreate(MainActivity.java:55) The app crashes on start can anyone please suggest me what to do.
I looked around on both Google and StackOverflow and I have found some information. However, I have not got it to work. Now I hope that I can get help with my code.
I'm relatively inexperienced in Android development.
package com.example.appkhushveehoreca;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final int HOME_FRAGMENT = 0;
private static final int REWARDS_FRAGMENT = 1;
private static final int ACCOUNT_FRAGMENT = 2;
private static final int WISHLIST_FRAGMENT = 3;
private FrameLayout frameLayout;
private ImageView actionBarLogo;
private int currentFragment = -1;
private NavigationView navigationView;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
actionBarLogo = findViewById(R.id.actionBarLogo);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this,drawer,toolbar,R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
setFragment(new HomeFragment(),HOME_FRAGMENT);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
frameLayout = findViewById(R.id.main_frame_layout);
}
@Override
public void onBackPressed(){
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else{
if(currentFragment == HOME_FRAGMENT){
currentFragment = -1;
super.onBackPressed();
}else{
actionBarLogo.setVisibility(View.VISIBLE);
invalidateOptionsMenu();
setFragment(new HomeFragment(),HOME_FRAGMENT);
navigationView.getMenu().getItem(0).setChecked(true);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
if(currentFragment == HOME_FRAGMENT) {
getMenuInflater().inflate(R.menu.main, menu);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
// else{
// getSupportActionBar().setDisplayShowTitleEnabled(true);
// }
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.main_search_icon){
/// todo: search
return true;
}else if(id == R.id.main_notification_icon){
/// todo: notification
return true;
}else if (id == R.id.main_cart_icon){
/// todo: cart
return true;
}
return super.onOptionsItemSelected(item);
}
public void goToFragment(String title,Fragment fragment, int fragmentNo){
actionBarLogo.setVisibility(View.GONE);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(title);
invalidateOptionsMenu();
setFragment(fragment,fragmentNo);
// if(fragmentNo == REWARDS_FRAGMENT){
// navigationView.getMenu().getItem(1).setChecked(true);
// }
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
////// Handle navigation view clicks here ///////
int id = item.getItemId();
if(id == R.id.nav_my_khushveeHoreca){
actionBarLogo.setVisibility(View.VISIBLE);
invalidateOptionsMenu();
setFragment(new HomeFragment(),HOME_FRAGMENT);
}else if(id == R.id.nav_my_account){
goToFragment("My Account",new MyAccountFragment(),ACCOUNT_FRAGMENT);
}else if(id == R.id.nav_my_offers){
goToFragment("My Rewards",new MyRewardsFragment(),REWARDS_FRAGMENT);
}else if(id == R.id.nav_my_wishlist){
goToFragment("My WishList",new MyWishlistFragment(),WISHLIST_FRAGMENT);
}else if(id == R.id.nav_sign_out) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void setFragment(Fragment fragment,int fragmentNo){
if(fragmentNo != currentFragment) {
currentFragment = fragmentNo;
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(frameLayout.getId(),fragment);
fragmentTransaction.commit();
}
}
}