有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java工具栏标签和选项菜单未显示在我的应用程序中

我有这个问题已经有一段时间了,真的很烦人。我不确定是什么导致了它,多个构建以及新项目都没有修复它。以下是我的Java代码:

public class MainActivity extends AppCompatActivity {
    Boolean toastCheck = true;
    String name = "Not";
    String lastName = "Available";
    WebView webview;
    String password = "asdf123";
    SharedPreferences prefs = null;

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

        prefs = getSharedPreferences("com.amrapps.foodapp", MODE_PRIVATE);

        name = prefs.getString("name", "");
        lastName = prefs.getString("lastName", "");
        password = prefs.getString("password", "");

        // Check for null values and set default if empty
        if(name == "") {
            name = "Johnny";
        }

        if (lastName == "") {
            lastName = "Appleseed";
        }

        if (password == "") {
            password = "Asdf123";
        }

        webview = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webview.getSettings();

        Random random = new Random();
        int emailNum = random.nextInt(50) + 1;
        final String email = name.charAt(0) + lastName + emailNum + "@gmail.com";

        webview.loadUrl("https://www.panerabread.com/en-us/mypanera/registration-page.html");

        webSettings.setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);

        // Fill out form
        webview.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                WebView myWebView = (WebView) findViewById(R.id.webview);

                //hide loading image
                //findViewById(R.id.progressBar).setVisibility(View.GONE);
                //show WebView
                findViewById(R.id.webview).setVisibility(View.VISIBLE);

                myWebView.loadUrl("javascript:document.getElementById('join_first_name').value='" + name + "';void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('join_last_name').value='" + lastName + "';void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('join_email').value='" + email + "';void(0);");
                myWebView.loadUrl("javascript:document.getElementById('join_confirm_email').value='" + email + "';void(0);");
                myWebView.loadUrl("javascript:document.getElementById('join_password').value='" + password + "';void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('join_confirm_password').value='" + password + "';void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('phone_number_a').value='231';void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('phone_number_b').value='123';void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('phone_number_c').value='2310';void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('join_i_agree').click();void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('join_card_not_available').click();void(0); ");
                myWebView.loadUrl("javascript:document.getElementById('#join-now-primary'); ");

                myWebView.pageDown(true);
                // Make sure a toast is only shown once.
                while (toastCheck) {
                    Toast.makeText(MainActivity.this, "Click the \"join\" button.", Toast.LENGTH_SHORT).show();
                    toastCheck = false;
                }
            }
        });
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webview.loadUrl("https://delivery1.panerabread.com/#!/orderProcess/");
            }
        });

    }
    @Override
    public void onResume() {
        super.onResume();

        if(prefs.getBoolean("firstRun", true)) {
            //Intent myIntent = new Intent(this, firstRun.class);
            //startActivity(myIntent);

            prefs.edit().putBoolean("firstRun", false).commit();

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);
        return true;
    }

主要活动。xml:

<安卓.support.design.widget.CoordinatorLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:fitsSystemWindows="true"
    tools:context="com.amrapps.foodapp.MainActivity">

    <安卓.support.design.widget.AppBarLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:theme="@style/AppTheme.AppBarOverlay">

        <安卓.support.v7.widget.Toolbar
            安卓:id="@+id/toolbar"
            安卓:layout_width="match_parent"
            安卓:layout_height="?attr/actionBarSize"
            安卓:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </安卓.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <安卓.support.design.widget.FloatingActionButton
        安卓:id="@+id/fab"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="bottom|end"
        安卓:layout_margin="@dimen/fab_margin"
        安卓:src="@安卓:drawable/ic_dialog_email" />

</安卓.support.design.widget.CoordinatorLayout>

主要内容。xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.amrapps.foodapp.MainActivity"
    tools:showIn="@layout/activity_main">

    <WebView
        安卓:id="@+id/webview"
        安卓:layout_width="fill_parent"
        安卓:layout_height="fill_parent"
        安卓:text="Hello World!" />
</RelativeLayout>

关于为什么这不起作用,我唯一的线索是它与mainActivity java代码有关。在我从旧的故障项目复制粘贴到那里之前,它曾经工作得很好


共 (1) 个答案

  1. # 1 楼答案

    对于工具栏,请使用以下代码。工具栏后菜单也应出现在工具栏中

           Toolbar  mToolbar = (Toolbar) findViewById(R.id.toolbar);
    
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);