2012-09-23 79 views
0

EXI想在mywebview加載一個URL時,在選項菜單如何使用對象變量作爲全局/公共變量的Android

這裏的「物品1」最終用戶點擊是我的代碼:

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

謝謝!

+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); 
     } 
    } 
} 
+0

非常感謝您的回答,但您能否更加準確,您是否可以修改我的代碼:)我是一名完美的初學者! – Random78952

+0

@RochesterFox查看我的編輯。 –

+0

謝謝!我試試這個,對不起我的愚蠢請求 – Random78952