0

我正在尝试向菜单添加第二个链接。第一个工作正常。第二个叫做menu_about

package com.test.apppackage;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

    private WebView mWebview ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mWebview  = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("https://www.google.com");
        setContentView(mWebview );



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }



    public class MyWebViewClient extends WebViewClient {

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


    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }



    public class MyWebViewClient extends WebViewClient {

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


}
4

1 回答 1

1

你有 2 onOptionsItemSelected

public boolean onOptionsItemSelected(MenuItem item) { // <-- this
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }



    public class MyWebViewClient extends WebViewClient {

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


    public boolean onOptionsItemSelected(MenuItem item) { // <-- and this
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }

删除其中一个并像这样切换 id:

public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;


        }
        return super.onOptionsItemSelected(item);
    }
于 2013-01-02T17:33:32.650 回答