Android Studio:getSupportActionBar()返回null

时间:2015-11-18 02:11:38

标签: java android xml android-studio

我在尝试为Android应用程序设置Navigaion抽屉时遇到问题。这是我关注的教程。 http://www.recursiverobot.com/post/59404388046/implementing-the-new-navigation-drawer-in-android

这是我在尝试运行代码时遇到的错误:

尝试调用虚方法'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

我相信我的问题出现在我的xml代码中。这是我的onCreate()方法MainActivity.java:

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

    mTitle = "test";

    mPlanetTitles = new String[]{"Home", "Settings", "Application", "Contact Us"};
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    ActionBar myBar = getSupportActionBar();
    myBar.setDisplayHomeAsUpEnabled(true);
    myBar.setHomeButtonEnabled(true);

    /*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);*/


    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close){

        public void onDrawerClosed(View view){
            getActionBar().setTitle(mTitle);
        }

        public void onDrawerOpen(View drawerView){
            getActionBar().setTitle(mTitle);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener{
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
    }
}

和XML: drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!--drawer_list_item.xml-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="@+id/text_view"
>

</TextView>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--content_main.xml-->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111">

</ListView>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<!--activity_main.xml-->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

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

menu_main.xml

<!--menu_main.xml-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />

<item android:id="@+id/HiBen" android:title="@string/HiBen"
    android:orderInCategory="100" app:showAsAction="never" />

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!--AndroidManifeset.xml-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stateofnebraska.designstudio.mobileapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我为过多的问题道歉,但我不确定需要多少信息。我已尝试将content_main.xml中当前的代码放在acivity_main.xml中,仅使用getActionBar()进行了尝试。我相信这个问题可能出现在ActionBarDrawerToggle声明的某个地方。任何帮助将不胜感激。

0 个答案:

没有答案