Android菜单 - 项目未激活

时间:2016-03-07 18:56:25

标签: java android xml menu onclick

我制作了我的第一个Android应用程序,我想只在我的应用程序中显示小菜单的网页。但菜单是我的大问题。 这是我的content_main.xml中的代码,用于绘制我的菜单。

<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();
}

当我点击调用刷新方法的图标时,没有任何事情发生

2 个答案:

答案 0 :(得分:0)

该方法需要采取一种观点,例如:

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

答案 1 :(得分:0)

您必须将refresh()方法更改为如下所示:

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

它应该接受MenuItem作为参数,因为您试图从菜单中调用此方法。