【问题标题】:Android ActionBar missing after extending Appcompatactivity扩展 Appcompatactivity 后缺少 Android ActionBar
【发布时间】:2019-01-18 16:41:28
【问题描述】:

我最近在Activities 中更新了扩展Appcompatactivity 的应用程序。从那时起,当我启动外部库 Intent 时,Actionbar 就消失了。

例如,我使用HockeyApp SDK 来启动他们的FeedbackActivity

这是我的代码:

FeedbackManager.showFeedbackActivity(this, Uri.fromFile(file));

这里有一个截图(你可以看到ActionBar 不见了)。

在我开始扩展 Appcompatactivity 之前它曾经可以工作。

对于Activities 的其余部分,它可以工作。 ActionBar is gone 仅在我启动外部库时使用 Intent

有什么想法吗?

【问题讨论】:

    标签: android android-actionbar android-toolbar android-appcompat hockeyapp


    【解决方案1】:

    首先,检查您的主题,它可能如下所示(“NoActionBar”)。然后操作栏没有出现。如果这是你的问题。请为您的应用添加合适的主题

     <application
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        />
    

    如果您的主题没有问题,您可以将以下内容添加到您的 XML 文件中。 (将其添加为您的 XML 文件的第一个子文件)

    <android.support.v7.widget.Toolbar
       android:id="@+id/my_toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="?attr/colorPrimary"
       android:elevation="4dp"
       android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    

    并在创建方法上将以下内容添加到您的活动中

    protected void onCreate(Bundle savedInstanceState) {
        .......
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
    }
    

    【讨论】:

    • @Ale 现在有什么错误?相同或不同。请确保您将 Toolbar 添加为 xml 的第一个子项
    • 您的解决方案是正确的,但它在上下文中不起作用,因为我们正在启动一个外部 Intent。当我们启动 FeedbackManager.showFeedbackActivity(this, Uri.fromFile(file)); 时,它会打开一个新的 Activity(来自 HockeyApp SDK),但我们无法修改其布局。
    【解决方案2】:

    原因可能是FeedbackManager.showFeedbackActivity(this, Uri.fromFile(file))打开了一个新的FeedbackActivity.class,它是Activity.class的子类而不是AppCompatActivity.class,所以它不能显示ActionBar。这里有一个链接https://stackoverflow.com/questions/30681918/nullpointerexception-with-actionbar-setdisplayhomeasupenabledboolean-on-a-nu解释了一些原因。

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 2020-12-11
      • 2021-01-15
      • 2011-10-07
      • 2017-01-05
      • 2010-11-13
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多