删除actionBar会使我的应用程序崩溃(提示不幸的是,应用程序已停止)

时间:2018-11-13 15:24:02

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

我是应用开发人员的新手,一直没有找到解决此问题的方法。我正在为其创建一个包含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(){

`

2 个答案:

答案 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()

答案 1 :(得分:0)

我找到了一种解决方法。我所做的是:

在“项目”标签中,进入-res>值> 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>
相关问题