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

时间:2015-11-25 17:21:12

标签: java android xml

我已经有这个问题了一段时间,这真的很烦人。我不确定是什么导致它和多个构建以及使新项目没有修复它。这是我的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;
    }

activity_main.xml中:

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

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

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

    </android.support.design.widget.AppBarLayout>

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

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

</android.support.design.widget.CoordinatorLayout>

content_main.xml:

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

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

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

1 个答案:

答案 0 :(得分:1)

对于工具栏使用此代码。工具栏菜单也应出现在工具栏

       Toolbar  mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);