簡體   English   中英

findViewById在列表視圖上返回null

[英]findViewById returns null on list view

因此,出於某些未知原因(我也進行了搜索,檢查了是否在設置view之前沒有鍵入錯誤的ID或調用了它 ),當我嘗試獲取導航抽屜的列表視圖時,調用findViewById返回null。 我目前也在本教程的“初始化抽屜列表”一節中。

但是,一旦調用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]

這是抽屜_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.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

下一行,所有listview的實現的代碼必須位於NavigationDrawerFragment中而不位於Activity中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM