如何将对象变量用作全局/公共变量Android

时间:2012-09-23 19:04:36

标签: android variables object webview global

exi想要在最终用户点击选项菜单中的“item1”时在mywebview中加载网址

这是我的代码:

package com.exemple.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.app.AlertDialog;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://m.google.com");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                //AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                //alertDialog.setTitle("Reset...");
                //alertDialog.setMessage("Are you sure?");
                //alertDialog.show();
                myWebView.loadUrl("http://m.google.com");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:2)

只需更改

WebView myWebView = (WebView) findViewById(R.id.webview);

myWebView = (WebView) findViewById(R.id.webview);

并将以下内容添加为类变量:

WebView myWebView;

所以你的最终代码变成了:

public class MainActivity extends Activity {

    WebView myWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://m.google.info"); //You may want to comment this out, as you said you wanted to load it only when the user clicked
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                //AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                //alertDialog.setTitle("Reset...");
                //alertDialog.setMessage("Are you sure?");
                //alertDialog.show();
                myWebView.loadUrl("http://m.google.com");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}