Ошибка Попытка вызвать виртуальный метод. на нулевой ссылке на объект - PullRequest
0 голосов
/ 04 февраля 2020

это не работает после выполнения проекта и очистки кода. Я получаю следующую ошибку. Попытка вызвать виртуальный метод void 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>

Ответы [ 2 ]

2 голосов
/ 04 февраля 2020

Вы используете Theme.AppCompat.Light.NoActionBar в стилях. xml, поэтому getSupportActionBar() возвращает ноль, проверьте стили. xml file & измените NoActionBar на DarkActionBar.

Полные стили. 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>


0 голосов
/ 04 февраля 2020

В вашем styles.xml изменении файла

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.NoActionBar">

на

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">

Ваш текущий AppBaseTheme использует тему без ActionBar, поэтому ваш вызов .getSupportActionBar() возвращает ноль потому что ваша тема не имеет панели действий.

...