2013-01-02 51 views
0

我想添加第二個鏈接到菜單。第一個工作正常。第二個叫menu_about試圖添加第二個鏈接到應用程序菜單android

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


} 
+0

描述資源路徑位置類型重複的方法onOptionsItemSelected( MenuItem)類型MainActivity MainActivity.java第47行Java問題 –

+0

此代碼不會編譯,因爲您已經複製了您的方法和您的內部最後一堂課。您需要將第二個onOptionsItemSelected方法中的case添加到第一個switch語句中。我建議你閱讀一下switch語句,或者一般的Java。 – qzikl

回答

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

謝謝sooooooo多 –

+0

沒問題..... – Ahmad

相關問題