因为某些未知原因(我也搜索过,检查我是否输入了错误的ID或calling it before setting the view),调用 findViewById 返回null当我尝试获取导航抽屉的列表视图时。我目前也在this tutorial上关注" Inicialize the drawer list"。
但是,一旦我调用 findViewById ,我就会得到一个 null 引用,当我调用<时,会引起 null引用异常 em> 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 :(得分:0)
以下行和所有listview实现的代码必须在NavigationDrawerFragment中,而不是在Activity
中mDrawerList = (ListView) findViewById(R.id.drawer_list);