片段导航抽屉 - MainActivity中的findViewById返回null

时间:2015-11-13 08:44:52

标签: android android-fragments

我正在尝试使用导航抽屉创建一个Android视图。

我无法在listview中添加项目。

my fragment_navigation_drawer.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:choiceMode="singleChoice"
android:divider="@android:color/transparent" android:dividerHeight="0dp"
android:background="#cccc" tools:context=".NavigationDrawerFragment"
android:id="@+id/drawerlist"
android:clickable="true"
tools:listitem="@android:layout/simple_list_item_1" />

在MainActivity.java中,在onCreate()中我称之为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    //This is not working but it should
    createLateral();
}

private void createLateral(){
    ListView mDrawerList = (ListView) findViewById(R.id.drawerlist);
    ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, menuLaterale);
    mDrawerList.setAdapter(mAdapter);
}

它给了我异常,因为findViewById(R.id.drawerlist)返回null。我该如何解决?

0 个答案:

没有答案