2017-07-10 2 views
0

Je suis en train de créer un programatically ActionBar dans un DrawerLayout comme suit:Comment créer un ActionBar/toolbar en Java

import android.support.v7.widget.Toolbar; 

public class RevToolBar extends MainActivity { 

    public Toolbar getRevToolbar() { 
     Toolbar revToolBar = new Toolbar(this); 
     setSupportActionBar(revToolBar); 

     return revToolBar; 
    } 
} 

Puis dans l'activité principale:

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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

     Toolbar toolbar = new RevToolBar().getRevToolbar(); 

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

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

Cependant, la app plante toujours juste après le démarrage:

App a arrêté
Ouvrez l'application à nouveau

Quelle est la bonne façon de créer et d'ajouter un ActionBar/barre d'outils en Java à un DrawerLayout?

Merci d'avance.

MISE À JOUR

La sortie lancé:

07/10 14:16:22: Launching app 
$ adb install-multiple -r -p com.example.rev.myapp /media/rev/5431214957EBF5D7/projects/android/myapp/app/build/intermediates/split-apk/debug/slices/slice_0.apk 
Split APKs installed 
$ adb shell am start -n "com.example.rev.myapp/com.example.rev.myapp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 
Client not ready yet..Waiting for process to come online 
Connected to process 9754 on device Nexus_5X_Edited_API_25 [emulator-5554] 
Application terminated. 
+0

mettre votre stacktrace ici –

+0

Merci pour la réponse @LuizFernandoSalvaterra. Je viens de faire une mise à jour et inclus cela. –

+0

Pourquoi n'utilisez-vous pas une barre d'outils dans le fichier de modèle? – Cochi

Répondre

1

essayer cette

public class RevToolBar 
{ 
Context mContext; 
public RevToolBar(Context context){ 
    mContext = context; 
    } 
public Toolbar getRevToolbar() { 
    Toolbar toolbar = new Toolbar(mContext); 
    LinearLayout.LayoutParams toolBarParams = new LinearLayout.LayoutParams(
      Toolbar.LayoutParams.MATCH_PARENT, 
      150 
    ); 
    toolbar.setLayoutParams(toolBarParams); 
    toolbar.setBackgroundColor(Color.BLUE); 
    toolbar.setVisibility(View.VISIBLE); 
    return toolbar; 
    } 
} 

et MainActivity

RevToolBar revToolBar = new RevToolBar(MainActivity.this); 
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
drawer .addView(revToolBar.getRevToolbar(), 0);