防止状态栏下拉

时间:2018-06-29 13:58:23

标签: android statusbar

在尝试使用正在开发的应用程序时,我一直试图防止下拉状态栏。

我正在尝试使用以下代码:

    View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

我一直收到此错误:java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.liambroughton.a900roomservice / com.example.liambroughton.a900roomservice.MainActivity}:java.lang.NullPointerException:尝试调用空对象引用上的虚拟方法'void android.app.ActionBar.hide()'

我猜我将代码粘贴到不正确的位置?但是话又说回来,我不确定应该在哪里粘贴代码。

这是我的代码(其中没有上面的代码):

public class MainActivity extends AppCompatActivity {
String ShowOrHideWebViewInitialUse = "show";
private WebView myWebView;
private ProgressBar spinner;

@Override
protected void onPause() {
    super.onPause();

    ActivityManager activityManager = (ActivityManager) getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);

    activityManager.moveTaskToFront(getTaskId(), 0);
}

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

    ImageButton mShowDialog = (ImageButton) findViewById(R.id.btnShowDialog);
    mShowDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
            View mView = getLayoutInflater().inflate(R.layout.dialog_login, null);
            final EditText mUsername = (EditText) mView.findViewById(R.id.etUsername);
            final EditText mPassword = (EditText) mView.findViewById(R.id.etPassword);
            Button mLogin = (Button) mView.findViewById(R.id.btnLogin);

            mLogin.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view_) {
                    if(mUsername.getText().toString().equals("admin") && mPassword.getText().toString().equals("Lum1n1te")){
                        Toast.makeText(MainActivity.this,
                                getString(R.string.success_login_msg),
                                Toast.LENGTH_SHORT).show();
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                        startActivity(
                                new Intent(Settings.ACTION_SETTINGS));
                    }else{
                        Toast.makeText(MainActivity.this,
                                getString(R.string.error_login_msg),
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

            mBuilder.setView(mView);
            AlertDialog dialog = mBuilder.create();
            dialog.show();
        }
    });

    myWebView = (WebView) findViewById(R.id.webView);

    spinner = (ProgressBar) findViewById(R.id.progressBar1);
    myWebView.setWebViewClient(new CustomWebViewClient() {
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    });

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.getSettings().setDomStorageEnabled(true);
    myWebView.getSettings().setDatabaseEnabled(true);
    myWebView.getSettings().setMinimumFontSize(1);
    myWebView.getSettings().setMinimumLogicalFontSize(1);
    myWebView.setWebChromeClient(new WebChromeClient());
    myWebView.loadUrl("https://node-red-test.ftp21.net:2024/ui");
}

@Override
public void onBackPressed() {
    // nothing to do here
    // … really
}

// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView myWebView, String url, Bitmap favicon) {
        // only make it invisible the FIRST time the app is run
        if (ShowOrHideWebViewInitialUse.equals("show")) {
            myWebView.setVisibility(myWebView.INVISIBLE);
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        ShowOrHideWebViewInitialUse = "hide";
        spinner.setVisibility(View.GONE);

        view.setVisibility(myWebView.VISIBLE);
        super.onPageFinished(view, url);

    }
}
}

我尝试了许多其他页面和指南,但都没有成功。

以下是我尝试仅供参考的一些页面。 No.1 No.2 No.3

0 个答案:

没有答案