2017-01-06 6 views
0

ОШИБКА:DrawerToggle может не отображаться, поскольку NavigationIcon не видно

DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);

public class Homepage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

    int storeid; 
    String storename; 
    private RecyclerView recyclerView; 
    ProductAdapter adapter; 
    private RecyclerView.LayoutManager layoutManager; 
    ArrayList<ProductClass> Products = new ArrayList<>(); 
    Database database; 
    public DrawerLayout mDrawerLayout; 
    public ActionBarDrawerToggle mDrawerToggle; 
    ActionBar actionBar; 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1); 
     setSupportActionBar(toolbar); 
     database = new Database(); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(
       this, mDrawerLayout,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 

     assert mDrawerLayout != null; 
     mDrawerLayout.addDrawerListener(mDrawerToggle); 
     mDrawerToggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(Homepage.this); 


     load(); 

     View hView = navigationView.getHeaderView(0); 
     TextView nav_name = (TextView)hView.findViewById(R.id.textView); 
     nav_name.setText(storename); 

     Products = database.getstoreproducts(storeid); 

     if (Products == null) 
      Toast.makeText(Homepage.this, "Store is empty", Toast.LENGTH_LONG).show(); 
     else { 

      recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 
      recyclerView.setHasFixedSize(true); 
      layoutManager = new LinearLayoutManager(this); 
      recyclerView.setLayoutManager(layoutManager); 
      adapter = new ProductAdapter(Products, Homepage.this); 
      recyclerView.setAdapter(adapter); 

     } 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 


    @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); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater m = getMenuInflater(); 
     return true; 
    } 

    public void load() { 

     SharedPreferences sharedPreferences = getSharedPreferences("MyStore", Context.MODE_PRIVATE); 
     storename = sharedPreferences.getString("storename", "Default"); 
     storeid = sharedPreferences.getInt("storeid", -1); 

    } 


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

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

     if (id == R.id.nav_home) { 

      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      if (drawer.isDrawerOpen(GravityCompat.START)) { 
       drawer.closeDrawer(GravityCompat.START); 
      } 

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

      Intent intent = new Intent(Homepage.this, addnewproduct.class); 
      startActivity(intent); 

     } 

     else if (id == R.id.nav_user) { 

      Intent intent = new Intent(Homepage.this, storeinfo.class); 
      startActivity(intent); 

     } 

     else if (id == R.id.nav_logout) { 

      SharedPreferences sharedPreferences = getSharedPreferences("MyStore", Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor=sharedPreferences.edit(); 
      editor.putString("logged", "unsuccessful"); 
      editor.commit(); 
      Intent intent = new Intent(Homepage.this, MainActivity.class); 
      startActivity(intent); 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

     return true; 
    } 

} 

T попробовал все, что я нашел, но не смог найти какое-либо решение. пожалуйста помоги.

+0

Добавить панель инструментов layout.xml content – Qamar

ответ

0

Вам необходимо добавить следующие строки для пользовательской навигации иконы,

getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
getSupportActionBar().setHomeAsUpIndicator(R.drawable.logo); 
0

добавить следующие строки после инициализации ActionBar в OnCreate()

actionbar.setDisplayHomeAsUpEnabled(true); 

Если это не сработает, попробуйте это :

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);