0

多年来,我一直在绞尽脑汁,但认为我离树林太近了,看不到树木。谁能告诉我为什么下面菜单中的item1不能刷新 web 视图?

(退出按钮(item2)工作得很好,如果这很重要。)

package com.my.project;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;

public class MyProjectActivity extends Activity
{
    final Activity activity = this;

        public void onCreate(Bundle savedInstanceState) 

            {
                super.onCreate(savedInstanceState);
                getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.main);       

                WebView WebView = (WebView) findViewById(R.id.webview);                         
                WebView.getSettings().setJavaScriptEnabled(true);
                WebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
                WebView.loadUrl("http://www.mydomain.php");
            }   

        // Create Menu Buttons
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
            {   
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.options_menu, menu);
                return true;
            }

        // Set Menu Button Actions
        @Override
        public boolean onOptionsItemSelected(MenuItem item) 
            {
            switch (item.getItemId()) 
                {
                    case R.id.item1:
                        reload();
                        return true;
                    case R.id.item2:     
                        finish();
                        return true;
                    default:
                        return super.onOptionsItemSelected(item);
                }
            }
}
4

1 回答 1

2

试试这个只是微小的变化:::

public class MyProjectActivity extends Activity
{
final Activity activity = this;
WebView webView;

    public void onCreate(Bundle savedInstanceState) 

        {
            super.onCreate(savedInstanceState);
            getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.main);       

            webView = (WebView) findViewById(R.id.webview);                         
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
            webView.loadUrl("http://www.mydomain.php");
        }   

    // Create Menu Buttons
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
        {   
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.options_menu, menu);
            return true;
        }

    // Set Menu Button Actions
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        {
        switch (item.getItemId()) 
            {
                case R.id.item1:
                    webView.reload();
                    break;
                case R.id.item2:     
                    finish();
                    break
            }
                    return super.onOptionsItemSelected(item);
        }
}
于 2012-04-16T17:26:17.433 回答