【问题标题】:Removing actionBar crashes my app (It says Unfortunately, app has stopped)删除 actionBar 使我的应用程序崩溃(它说不幸的是,应用程序已停止)
【发布时间】:2018-11-13 15:24:01
【问题描述】:

我是应用程序开发的新手,我一直在寻找解决此问题的方法,但无济于事。我正在为此创建一个包含 webview 的应用程序,并且我正在尝试删除我尝试使用 actionBar.hide();actionBar.setDisplayShowTitleEnabled(false); 的操作栏。我什至试过actionBar.setHideOnContentScrollEnabled(true);。它的作用是打开应用程序窗口,然后屏幕变黑,应用程序关闭并弹出[不幸的是,应用程序已停止]。

这是我的代码 `

import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {


    WebView webA;
    ProgressBar progressBar;

    private WebView myWebView;

    @Override
    public void onBackPressed() {
        if (webA.canGoBack()) {
            webA.goBack();
        }
    else {
        finish();
    }
}

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

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);//hide title


    webA = (WebView)findViewById(R.id.webA);
    webA.setWebViewClient(new WebViewClient()); // This ensures that it's opened in the webView not default browser
    webA.loadUrl("https://www.fiander.me");

    // Progress bar
    progressBar = findViewById(R.id.progressBar);
    progressBar.setMax(100);
    // WebView
    WebSettings webSettings = webA.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webA.setWebChromeClient(new WebChromeClient(){

`

【问题讨论】:

  • 我找到了修复它的方法。我所做的是:

标签: android android-layout android-studio-3.0


【解决方案1】:

在您的清单 AndroidManifest.xml 中,您可以定义应用的主题,例如:

android:theme="@style/AppTheme"

这个主题在styles.xml 中定义,类似于:

<style name="AppTheme" parent="Theme.AppCompat.Light.LightActionBar">

改成:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

所以你的应用中没有ActionBar
您可以删除此代码:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);//hide title

来自onCreate()

【讨论】:

  • 我从清单中尝试过。它没有用。当我从 style.xml 更改它时它终于起作用了
  • @OrangeUncle 我的回答是更改styles.xml 而不是清单
【解决方案2】:

我找到了修复它的方法。我所做的是:

在项目选项卡中,我进入了 -- res > values > styles.xml。

然后将父名称更改为 Theme.AppCompat.Light.NoActionBar 像这样:

<!-- 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2015-03-25
    • 2016-10-03
    • 2018-03-23
    • 2015-05-09
    • 2016-11-19
    相关资源
    最近更新 更多