2016-04-22 1 views
0

Donc, pour une raison inconnue (j'ai également cherché autour, vérifié si je ne tapais pas le mauvais ID ou calling it before setting the view), l'appel findViewById renvoie null lorsque j'essaie d'obtenir la liste vue d'un tiroir de navigation. Je suis également actuellement this tutorial, dans la section "Inicialiser la liste des tiroirs".findViewById renvoie null sur la vue liste

Cependant, une fois que je l'appelle findViewById, je reçois un nulle référence, ce qui provoque plus tard, une exception de référence null quand je l'appelle la méthode de setAdapter.

Alors, voici le code en question:

public class DrawerActivity extends AppCompatActivity 
     implements NavigationDrawerFragment.NavigationDrawerCallbacks, AndroidFragmentApplication.Callbacks { 

    [Needless stuff here] 

    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private CharSequence mDrawerTitle; 
    private ListView mDrawerList; 
    private String[] mListItems; 

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


     mListItems = getResources().getStringArray(R.array.list_values); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.drawer_list); //this returns null 
     //null pointer exception happens next 
     mDrawerList.setAdapter(
       new ArrayAdapter<>(
         this, R.layout.drawer_list_item, mListItems 
       ) 
     ); 
     [More following, but it isn't needed] 

Et ici va le drawer_activity.xml:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical"> 

    <!-- placed here as explained @ 
      http://developer.android.com/training/appbar/setting-up.html--> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:elevation="4dp" 
     android:theme="@style/AppTheme.AppBarOverlay" 
     app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="pt.bluecover.wearable3d.DrawerActivity"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 


      <!-- As the main content view, the view below consumes the entire 
     space available using match_parent in both dimensions. --> 
      <FrameLayout 
       android:id="@+id/container" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 


     </LinearLayout> 

     <!-- 
     This here is the spin bar indicating that the program is loading something 
     --> 
     <ProgressBar 
      android:id="@+id/progressBar1" 
      style="?android:attr/progressBarStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="100dip" 
      android:layout_marginRight="100dip" 
      android:maxHeight="35dip" 
      android:minHeight="35dip" 
      android:minWidth="7dip" 
      android:maxWidth="7dip" 
      android:background="@android:color/transparent" 
      android:layout_centerHorizontal="true" 
      android:indeterminate="true" /> 

     <!-- The drawer is given a fixed width in dp and extends the full height of 
      the container. --> 
     <fragment 
      android:id="@+id/navigation_drawer" 
      android:name="pt.bluecover.wearable3d.NavigationDrawerFragment" 
      android:layout_width="@dimen/navigation_drawer_width" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      tools:layout="@layout/drawer_drawer" /> 



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

Et le drawer_drawer.xml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:theme="@style/AppTheme.NavigationDrawer" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_list" 
    android:layout_gravity="start" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    tools:context="pt.bluecover.wearable3d.NavigationDrawerFragment" /> 

Alors, que peut provoquer le retour de ladite méthode null?

+0

où est votre NavigationDrawerFragment – mdDroid

+0

Ouais, merci, je fini par déplacer la vue de la liste du fragment à l'activité xml =) –

Répondre

0

Après la ligne et tous listview le code Realted doivent être NavigationDrawerFragment pas dans l'activité

mDrawerList = (ListView) findViewById(R.id.drawer_list);