2016-04-22 48 views
0

因此,對於一些未知的原因(我還四處搜尋,檢查我是不是打字錯誤的ID或calling it before setting the view),呼叫findViewById返回null當我嘗試獲取列表爲空導航抽屜的視圖。我目前還在this tutorial之後的「官方抽屜列表」部分。findViewById返回的列表視圖

但是,一旦我稱之爲findViewById,我得到一個參考,後來導致空引用異常當我打電話的setAdapter方法。

所以這裏的問題代碼:

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] 

這裏而來的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> 

而且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" /> 

那麼,什麼可能導致所述方法返回null

+0

哪裏是你NavigationDrawerFragment – mdDroid

+0

是啊,謝謝,我結束了移動從片段列表視圖活動XML =) –

回答

0

行之後,所有列表視圖realted代碼必須在NavigationDrawerFragment不活動

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