2016-05-31 1 views
0

Dans mon application Android, j'utilise une barre d'outils personnalisée, et le findViewById() ne parvient pas à trouver la barre d'outils.Android findViewById pour la barre d'outils renvoyant null

J'ai regardé autour d'Internet à tous les problèmes semblables à celui-ci, et aucun d'entre eux ont semblé fonctionner.

Mon écran revient toujours Toast "barre d'outils a échoué ..."

Voici mon java onCreate():

protected Toolbar toolbar; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     if(toolbar != null) { 
      setSupportActionBar(toolbar); 
     } else { 
      Toast.makeText(Home.this, "Toolbar failed...", Toast.LENGTH_SHORT).show(); 
      Toast.makeText(Home.this,"", Toast.LENGTH_SHORT).show(); 
     } 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 
     Intent intent = getIntent(); 
     String action = intent.getAction(); 
     String type = intent.getType(); 

     if (Intent.ACTION_SEND.equals(action) && type != null) { 
      if ("text/plain".equals(type)) { 
       handleSendText(intent); // Handle text being sent 
      } 
     } 

    } 

Voici le fichier de mise en page xml:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <android.support.v7.widget.Toolbar 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      android:elevation="4dp" 
      android:id="@+id/tool_bar"/> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/home" 
      android:layout_below="@id/tool_bar" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Your Scripture:" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:layout_below="@+id/textView2" 
      android:layout_alignParentLeft="true" /> 

    <TextView 
     android:id="@+id/scripturecontainer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="You have not selected a scripture yet..." 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:layout_below="@+id/textView1" 
     android:layout_alignParentLeft="true" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="trywidget" 
     android:text="Try the Widget" 
     android:layout_below="@+id/scripturecontainer" 
     android:layout_alignParentLeft="true" /> 

    <Button 
     android:id="@+id/widget_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="change" 
     android:text="Select A Scripture" 
     android:layout_below="@+id/button2" 
     android:layout_alignParentLeft="true" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="suggest" 
     android:text="Suggest A Feature" 
     android:layout_below="@+id/widget_button" 
     android:layout_alignParentLeft="true" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="wallpaper" 
     android:text="Change the Wallpaper" 
     android:layout_below="@+id/button3" 
     android:layout_alignParentLeft="true" /> 
    </RelativeLayout> 
    <ListView android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"/> 

</android.support.v4.widget.DrawerLayout> 

Voilà mon style xml fichier:

<resources> 

    <!-- 
     Base application theme, dependent on API level. This theme is replaced 
     by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
    --> 
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- 
      Theme customizations available in newer API levels can go in 
      res/values-vXX/styles.xml, while customizations related to 
      backward-compatibility can go here. 
     --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="windowNoTitle">true</item> 
     <item name="windowActionBar">false</item> 
    </style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    </style> 

</resources> 
+0

Cela ne semble pas être la mise en page que vous configurez pour que 'Activity', car il n » t avoir un 'ViewGroup' avec l'ID' container', et que 'FragmentTransaction' causerait un crash sans cela lors de la première exécution. –

+0

Il ne plante pas. Renvoie juste un null. –

+0

Je ne sais pas ce que vous entendez par "renvoie une valeur nulle", mais que "FragmentTransaction" tomberait en panne si la disposition actuelle n'avait pas 'container'. Étant donné que, et le fait que 'findViewById()' retourne 'null' pour votre' Toolbar', je dirais que la mise en page que vous avez posté n'est pas 'activity_home'. –

Répondre

1
<android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

Utilisez le code ci-dessus pour déclarer la barre d'outils dans votre xml et modifier le

android:theme="your_theme" 
+0

Cela vient de renvoyer une exception qui dit qu'il n'a pas pu trouver la vue android.support.design.widget.AppBarLayout. –

+0

@ sub6resources Ensuite, il vous manque 'compile' com.android.support:design:23.2.1" 'dans votre build.gradle –