在进行项目清理和代码清理之后,这是不起作用的,因为执行以下错误尝试调用空对象引用上的虚拟方法‘androidx.appcompat.app.ActionBar.setCustomView(android.view.View,androidx.appcompat.app.ActionBar$LayoutParams)’‘
final ActionBar abar = getSupportActionBar();
//line under the action bar
View viewActionBar = getLayoutInflater().inflate(R.layout.app_bar_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
textviewTitle.setText("WizChat");
assert abar != null;
abar.setCustomView(viewActionBar, params);
abar.setDisplayShowCustomEnabled(true);
abar.setDisplayShowTitleEnabled(false);
abar.setDisplayHomeAsUpEnabled(true);
abar.setIcon(R.color.background_color);
abar.setHomeButtonEnabled(true);
您可以在这里看到代码,这是您所要求的清单文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.logindesign"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.logindesign.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.logindesign.Welcome"/>
</application>
</manifest>
您可以在这里看到代码,这是您所要求的我的样式文件。
<resources>
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.NoActionBar">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
发布于 2020-02-04 05:13:59
在您的styles.xml
文件更改中
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.NoActionBar">
至
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
当前的AppBaseTheme
使用没有ActionBar的主题,所以对.getSupportActionBar()
的调用返回null,因为主题没有操作栏。
发布于 2020-02-04 05:07:49
您在styles.xml中使用的是styles.xml,这就是为什么getSupportActionBar()
返回null,检查styles.xml文件&将NoActionBar
更改为DarkActionBar
。
完整的styles.xml文件在这里
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
https://stackoverflow.com/questions/60055350
复制