当前位置: 首页 > 知识库问答 >
问题:

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

萧亦
2023-03-14

我试图隐藏Actionbar并改用工具栏,但如果我更改Theme.appcompat.light.NoActionBar,它确实会隐藏,但应用程序在行setContentView(r.layout.activity_main)处崩溃;

尽管它确实使用getSupportActionBar()隐藏。hide();但我不能用这个代码

Toolbar Toolbar=(Toolbar)findViewById(r.id.Toolbar);

setSupportActionBar(工具栏);

因为应用程序在setSupportActionBar(工具栏)崩溃:

主要活动:

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>

如果我在样式theme.appcompat.light.NoActionbar中使用NoActionbar,那么我的应用程序崩溃和catlog是:

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.access$1100(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
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'
}

更新

共有1个答案

计和顺
2023-03-14
<?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>
<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>
 类似资料:
  • 问题内容: 为了使用webdriver.io测试我的React应用,我需要使用phantomjs启动它。 起初我以为问题出在webdriver.io,但我意识到当我尝试渲染时PhantomJS返回一个空白页。 为了进行一些测试,我编写了这个javascript文件: 我这样启动: 但是不管我做什么,总是空虚。 我目前使用React 0.14.7和phantomjs 2.1.1。有人对我为什么无法渲

  • 我有一个主活动MainActivity,它是我的android应用程序的入口点。它通过发射器正确发射。然而,当我尝试执行startActivity时,我看到活动试图启动的一些调试,一些代码正确启动,但MainActivity从未启动。 我认为这可能与意图有关: 对吗? 从内部活动来看,我有 我甚至看到 10-11 22:23:46.026:INFO/ActivityManager(472):从pi

  • 我正在使用Reverfit2,我试图从我用Python制作的Web服务中请求一些数据。 实际上,它抛出了一个“java.lang.IllegalStateException”,但是当我调用GET方法时,API返回代码200,而且我还可以看到用于调试响应JSON的打印版本。问题是Call方法,因为它总是在failure时执行。 求求你,救命! 这是我的(简单的)Python Web服务,API-Pr

  • fReceiveBuffer是否存在一个视频帧? 哦,这里是我的FFMPEG初始化代码,需要打开相关视频解码器:http://paste.ubuntu.com/12529760//

  • 我对一些代码进行了分析,并对花费了这么多时间感到惊讶。 但我发现这快了大约5倍: 下面是Math.min的代码以供参考: 注意:我的用例是对称的,上面的情况都适用于max,而不是min。 我在我的应用程序中添加了以下代码,以尝试获得更好的数据: 这将打印每个新触摸事件的运行比率。当我在屏幕上旋转手指时,首先记录的比率是或或。这让我觉得这个测试不是很准确地测量时间。随着收集到更多的数据,比率往往在和

  • OS:Windows 10 JDK:jdk1。8.0_65 IDE:Netbeans 8.2 我采取的步骤: 1。我安装了Netbeans,选择了ALL列,这意味着它可以支持Groovy或grails应用程序 2。我还安装了Grails的最新版本,即Grails-3.3.5 3。安装完所有东西后,我打开了Netbeans。 我在NETBEANS 文件中采取的步骤 这里的问题是我无法创建一个新项目。