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

时间:2017-02-14 16:31:29

标签: android android-layout android-support-library android-support-design

我使用的是support.v7.widget.Toolbar,它无法通过R.id.xyzname findviewbyid找到工具栏。工具栏和工具栏都返回nil。所以代码崩溃与NullPointer在  setSupportActionBar(栏)。

在使用版本23的设备中进行测试时,相同的应用程序可以正常工作;使用版本为16的设备进行测试时,代码会与NullPointer崩溃。

这是我的活动

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>

1 个答案:

答案 0 :(得分:1)

使用v7支持工具栏支持较低的设备。喜欢这个

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