使用 findviewbyid android.support.v7.widget.Toolbar 访问工具栏时出现 NullPointer

NullPointer when accessing toolbar with findviewbyid android.support.v7.widget.Toolbar

我正在使用支持。v7.widget.Toolbar,但无法通过 R.id.xyzname findviewbyid 找到工具栏。工具栏和工具栏 return 均无。所以代码在 NullPointer 处崩溃 setSupportActionBar(工具栏).

同一个应用程序在版本 23 的设备上测试时工作正常;使用版本 16 的设备进行测试时,代码会因 NullPointer 而崩溃。

这是我的 activity

class classActivity extends AppCompatActivity {

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int currentApiVersion = Build.VERSION.SDK_INT;

        mOpenHelper = new IncidentOpenHelper(getApplicationContext());


        setContentView(R.layout.login);

        AppBarLayout appBarLayout   = (AppBarLayout) View.inflate(this, R.layout.app_toolbar, null);
        // [the above code helped me solve the issue]

        android.support.v7.widget.Toolbar toolbars = (android.support.v7.widget.Toolbar) appBarLayout.findViewById(R.id.app_bar_toolbar);



        setSupportActionBar(toolbars);


        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
}

这是我的layout.xml

<?xml version="1.0" encoding="utf-8"?>
<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="session_management.LoginActivity">


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

<!--
    <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
            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:id="@+id/app_bar_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>

-->

    <LinearLayout


  </Linear layout>





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

这是我的依赖项

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.http-client:google-http-client-jackson2:1.20.0'
compile 'com.google.api-client:google-api-client-android:1.20.0'
compile 'com.google.code.gson:gson:2.4'
compile project('libraries:volley')
/*                                                                                                                                                                                                                                                                                                                                                                        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
                                                                                                                                                                                                                                                                                                                                                                                */
compile 'com.google.android.gms:play-services-appindexing:8.4.0'
compile project(':cloudendpoint')

}

这是我的 app_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.Toolbar
    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:id="@+id/app_bar_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>

使用 v7 支持工具栏支持低端设备。像这样

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar_toolbar);