我正在使用support.v7.widget.Toolbar,它無法通過R.id.xyzname findviewbyid找到工具欄。工具欄和工具欄都返回nil。所以代碼崩潰與空指針 setSupportActionBar(工具欄)。當使用findviewbyid訪問工具欄時出現NullPointer android.support.v7.widget.Toolbar
在23版本的設備中測試時,相同的應用程序工作正常;該代碼與設備配版16
這裏進行測試時,與空指針崩潰是我的活動
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>
我一直在爲這兩天掙扎着。已經搜索過並閱讀了關聯的Google文檔。任何指導或指針非常感謝。 –
不理解倒票。你能解釋爲什麼倒票嗎? –
最後解決它後,充氣工具欄。 android.support.v7.widget.Toolbar toolbars =(android.support.v7.widget.Toolbar)layout1.findViewById(R.id.app_bar_toolbar);我更新了我的代碼。 –