如何将Android Studio中的主题从Noactionbar更改为ActionBar

发布于 2025-02-05 11:17:16 字数 7527 浏览 2 评论 0原文

我在Android Studio启动了一个项目,显然,主题是theme.nameofproject.noactionbar。

这引起了以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weoutdoor/com.example.weoutdoor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference

project themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WeOutdoor" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

<style name="Theme.WeOutdoor.NoActionBar">
    <item name="windowActionBar">true</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="Theme.WeOutdoor.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="Theme.WeOutdoor.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

和androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.weoutdoor">

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.WeOutdoor.AppBarOverlay"
    tools:targetApi="31">
    <activity
        android:name=".RegisterActivity"
        android:exported="false" />
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:theme="@style/Theme.WeOutdoor.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".UserProfileActivity"
        android:exported="true"
        android:theme="@style/Theme.WeOutdoor.AppBarOverlay">

    </activity>
    <activity android:name=".LoginActivity"
        android:exported="true"
        android:theme="@style/Theme.WeOutdoor.AppBarOverlay">

    </activity>

</application>

我尝试将样式的父和主题更改为themoverlay.appcompat.dark.actionalbar

,但是每当我尝试使用代码getsupportactionActionBar()。 settitle(“ weoutdoor”);

我在上面有上述错误。

在尝试在此帖子的答案中应用一些建议后,我最终遇到了一个新错误:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

目前的主题如下:

   <resources xmlns:tools="http://schemas.android.com/tools">
   <!-- Base application theme. -->
   <style name="MyTheme" 
   parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="windowActionBar">true</item>
   

以及我的androidMenifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.weoutdoor">

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/MyTheme"
    tools:targetApi="31">
    <activity
        android:name=".RegisterActivity"
        android:theme="@style/MyTheme"
        android:exported="false" />
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:theme="@style/MyTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".UserProfileActivity"
        android:exported="true"
        android:theme="@style/MyTheme">

    </activity>
    <activity android:name=".LoginActivity"
        android:exported="true"
        android:theme="@style/MyTheme">

    </activity>

</application>

请记住,在每个活动中,我都使用getsupportactionbar();

我在错误中突出显示的登录性:

public class LoginActivity extends AppCompatActivity {

private EditText editTextLoginEmail, editTextLoginPwd;
private ProgressBar progressBar;
private FirebaseAuth authProfile;
private static final String TAG = "LoginActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.login_activity);

    getSupportActionBar();

    editTextLoginEmail = findViewById(R.id.editText_login_email);
    editTextLoginPwd = findViewById(R.id.editText_login_password);

    progressBar = findViewById(R.id.progressBar);

    authProfile = FirebaseAuth.getInstance();

    //Login Tab
    Button buttonLogin = findViewById(R.id.button_login);
    buttonLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String textEmail = editTextLoginEmail.getText().toString();
            String textPwd = editTextLoginPwd.getText().toString();

            if(TextUtils.isEmpty(textEmail)){
                Toast.makeText(LoginActivity.this, "Please enter your email", Toast.LENGTH_SHORT).show();
                editTextLoginEmail.setError("Email is required");
                editTextLoginEmail.requestFocus();
            } else if (!Patterns.EMAIL_ADDRESS.matcher(textEmail).matches()){
                Toast.makeText(LoginActivity.this, "Please re-enter your email", Toast.LENGTH_SHORT).show();
                editTextLoginEmail.setError("Valid Email is required");
                editTextLoginEmail.requestFocus();
            } else if (TextUtils.isEmpty(textPwd)){
                Toast.makeText(LoginActivity.this, "Please enter your password", Toast.LENGTH_SHORT).show();
                editTextLoginPwd.setError("Password is required");
                editTextLoginPwd.requestFocus();

            } else {
                progressBar.setVisibility(View.VISIBLE);
                loginUser (textEmail, textPwd);

            }
        }
    });

在登录中很明显。java,有些行被评论并引起了NPE。

所有校正和动作栏都可以正常工作。

I started a project in Android Studio and apparently, the Theme was Theme.NameOfProject.NoActionBar.

This is causing a the following error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weoutdoor/com.example.weoutdoor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference

The project themes.xml :

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WeOutdoor" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

<style name="Theme.WeOutdoor.NoActionBar">
    <item name="windowActionBar">true</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="Theme.WeOutdoor.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="Theme.WeOutdoor.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

And the AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.weoutdoor">

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.WeOutdoor.AppBarOverlay"
    tools:targetApi="31">
    <activity
        android:name=".RegisterActivity"
        android:exported="false" />
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:theme="@style/Theme.WeOutdoor.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".UserProfileActivity"
        android:exported="true"
        android:theme="@style/Theme.WeOutdoor.AppBarOverlay">

    </activity>
    <activity android:name=".LoginActivity"
        android:exported="true"
        android:theme="@style/Theme.WeOutdoor.AppBarOverlay">

    </activity>

</application>

I tried to change the style parent and theme to ThemeOverlay.AppCompat.Dark.ActionBar

but every time i try to use the code getSupportActionBar().setTitle("WeOutdoor");

I get the mentioned error above.

After trying to apply some of the suggestions in the answers to this post I ended up with a new error :

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Currently my themes is as following :

   <resources xmlns:tools="http://schemas.android.com/tools">
   <!-- Base application theme. -->
   <style name="MyTheme" 
   parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="windowActionBar">true</item>
   

And my AndroidMenifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.weoutdoor">

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/MyTheme"
    tools:targetApi="31">
    <activity
        android:name=".RegisterActivity"
        android:theme="@style/MyTheme"
        android:exported="false" />
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:theme="@style/MyTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".UserProfileActivity"
        android:exported="true"
        android:theme="@style/MyTheme">

    </activity>
    <activity android:name=".LoginActivity"
        android:exported="true"
        android:theme="@style/MyTheme">

    </activity>

</application>

Keep in mind that in each activity.java i use getSupportActionBar();

My loginActivity that i highlighted in the error :

public class LoginActivity extends AppCompatActivity {

private EditText editTextLoginEmail, editTextLoginPwd;
private ProgressBar progressBar;
private FirebaseAuth authProfile;
private static final String TAG = "LoginActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.login_activity);

    getSupportActionBar();

    editTextLoginEmail = findViewById(R.id.editText_login_email);
    editTextLoginPwd = findViewById(R.id.editText_login_password);

    progressBar = findViewById(R.id.progressBar);

    authProfile = FirebaseAuth.getInstance();

    //Login Tab
    Button buttonLogin = findViewById(R.id.button_login);
    buttonLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String textEmail = editTextLoginEmail.getText().toString();
            String textPwd = editTextLoginPwd.getText().toString();

            if(TextUtils.isEmpty(textEmail)){
                Toast.makeText(LoginActivity.this, "Please enter your email", Toast.LENGTH_SHORT).show();
                editTextLoginEmail.setError("Email is required");
                editTextLoginEmail.requestFocus();
            } else if (!Patterns.EMAIL_ADDRESS.matcher(textEmail).matches()){
                Toast.makeText(LoginActivity.this, "Please re-enter your email", Toast.LENGTH_SHORT).show();
                editTextLoginEmail.setError("Valid Email is required");
                editTextLoginEmail.requestFocus();
            } else if (TextUtils.isEmpty(textPwd)){
                Toast.makeText(LoginActivity.this, "Please enter your password", Toast.LENGTH_SHORT).show();
                editTextLoginPwd.setError("Password is required");
                editTextLoginPwd.requestFocus();

            } else {
                progressBar.setVisibility(View.VISIBLE);
                loginUser (textEmail, textPwd);

            }
        }
    });

It would be obvious in loginActivity.java that some lines were commented out and were causing the NPE.

All corrected and ActionBar is working fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-02-12 11:17:16

您正在尝试使用themoverlay,它与完整的主题不同。但是,老实说,只需坚持noactionbar主题,然后在顶部创建自己的工具栏即可。它只是标题中包含textView的布局,以及一些image button s for Icons。

You're trying to use a ThemeOverlay, which isn't the same as a full Theme. But, honestly, just stick with the NoActionBar theme and create your own toolbar at the top. It's just a layout containing a TextView for the title and some ImageButtons for icons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文