Android - 为什么我不能在我的应用程序中使用 NoActionBar

Android - Why I can't use NoActionBar in my application

我试图隐藏操作栏并改用工具栏,但是如果我更改 Theme.AppCompat.Light.NoActionBar 它会隐藏但是应用程序在 setContentView(R.layout.activity_main);

行崩溃

尽管它确实使用此 getSupportActionBar().hide(); 隐藏,但我无法使用此代码

工具栏 toolbar = (工具栏)findViewById(R.id.toolbar);

setSupportActionBar(工具栏);

因为应用程序在 setSupportActionBar(toolbar) 处崩溃:

主要活动:

package com.example.unchallenged.news;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.unchallenged.news.MainActivity">


</RelativeLayout>

Style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>



</resources>

清单

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

minSdkVersion 19 targetSdkVersion 26

如果我使用 Theme.AppCompat.Light.NoActionBar 风格的 NoActionbar,那么我的应用程序崩溃和目录是:

07-13 13:26:53.781 18978-18978/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.unchallenged.news, PID: 18978
                                                   java.lang.NoSuchFieldError: No static field title of type I in class Landroid/support/v7/appcompat/R$id; or its superclasses (declaration of 'android.support.v7.appcompat.R$id' appears in /data/app/com.example.unchallenged.news-2/base.apk)
                                                       at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:482)
                                                       at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:325)
                                                       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:286)
                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                       at com.example.unchallenged.news.MainActivity.onCreate(MainActivity.java:11)
                                                       at android.app.Activity.performCreate(Activity.java:6877)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3208)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351)
                                                       at android.app.ActivityThread.access00(ActivityThread.java:222)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:158)
                                                       at android.app.ActivityThread.main(ActivityThread.java:7230)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-13 13:26:53.781 18978-18978/? I/Process: Sending signal. PID: 18978 SIG: 9

Build.grade(模块:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.unchallenged.news"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
}

已更新

您的 activity_main.xml 应如下所示。

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
        tools:context="com.example.unchallenged.news.MainActivity">

     <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </RelativeLayout>

更新

编辑您的style.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

编辑您的 MainActivity:

package com.example.unchallenged.news;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}

和activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
        tools:context="com.example.unchallenged.news.MainActivity">

     <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </RelativeLayout>

如果错误仍然存​​在,请检查 gradle.build 文件中的支持库版本。可以是这样的:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

编辑您的 post 并提供当前版本,之后我们可以尝试其他方法。

正如 Mohammed Junaid 所说。我使用的是“26.0.0 - alpha1”库版本,所以它可能有一些问题,所以改回“25.3.1”,它现在可以工作了。