简体   繁体   中英

Android menu - items not active

I make my first android app, and i would like to only show webpage in my app with small menu. But menu is my big problem. This is my code in my content_main.xml, that draw my menu.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <io.github.yavski.fabspeeddial.FabSpeedDial
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        app:fabGravity="bottom_start"
        app:fabMenu="@menu/menu_main"
        app:miniFabBackgroundTint="@android:color/holo_blue_bright"
        app:miniFabDrawableTint="?attr/colorPrimary"
        app:miniFabTitleTextColor="?attr/colorPrimaryDark" />

</FrameLayout>

menu_main.xml

<menu 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"
tools:context=".MainActivity">

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_refresh_white_24px"
    android:title="@string/menu_item_refresh"
    android:onClick="refresh"
/>
<item
    android:id="@+id/action_share"
    android:icon="@drawable/ic_share_white_24px"
    android:title="@string/menu_item_share"
    android:onClick="refresh"/>
<item
    android:id="@+id/action_about"
    android:icon="@drawable/ic_about_white_24px"
    android:title="@string/menu_item_about"
    android:onClick="refresh"/>
<item
    android:id="@+id/action_exit"
    android:icon="@drawable/ic_exit_white_24px"
    android:title="@string/menu_item_exit"
    android:onClick="refresh"/>
</menu>

MainActivity.java:

package com.example.roman.projectkosican;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */

final Activity activity = this;
private WebView webView = null;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

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


    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);


    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if (progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String         description, String failingUrl) {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl("http://dniobce.sk");
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}

@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.example.roman.projectkosican/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.example.roman.projectkosican/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}

public void refresh(View v) {
    webView.loadUrl("http://www.google.com");
    webView.reload();
}

When i click on icon which calls refresh method, nothing happends

The method needs to take a view eg:

public void refresh(View v) {
    webView.loadUrl("http://www.google.com");
    webView.reload();
}

You have to change your refresh() method to look like this :

 public void refresh(MenuItem menuItem){
         webView.loadUrl("http://www.google.com");
         webView.reload();
    }

It should accept MenuItem as an argument since you are trying to call this method from menu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM