2016-10-04 4 views
-2

私は私のmenu.xmlに次のコードを持っている:起動するウェブサイト

<item 
     android:id="@+id/more" 
     android:orderInCategory="100" 
     android:title="@string/goToWebsite" 
     app:showAsAction="ifRoom" /> 

public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       onBackPressed(); 
       return true; 
      case R.id.more: 
       goToUrl("http://www.example.com"); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

場合R.id.moreは実行されません。

goToUrl("http://www.example.com"); 
       return true; 

private void goToUrl (String url) { 
     Uri uriUrl = Uri.parse(url); 
     Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); 
     startActivity(launchBrowser); 
    } 

間違っているのですか?

更新:r.id.more下

Toasingに動作していないaswelel:

case R.id.more: 
       Toast.makeText(this,"TEST", Toast.LENGTH_LONG).show(); 
+0

が私の答え –

+0

をチェック願って、あなたはすべてのエラーを得ていますか? –

答えて

0

は解像度のメニューフォルダにMenu.xmlファイルを作成しますディレクトリ:

 <?xml version="1.0" encoding="utf-8"?> 
     <menu xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto"> 


      <item 
       android:id="@+id/common_menu_settings" 
       android:icon="@drawable/settings" 
       android:title="@string/settings" 
       app:showAsAction="ifRoom" /> 
      <item 
       android:id="@+id/more" 
       android:title="@string/goToWebsite" 
       app:showAsAction="ifRoom" /> 

</menu> 

アクティビティのonOptionsItemSelected()をオーバーライドします。

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
      case R.id.common_menu_settings: 
       onBackPressed(); 
       break; 
      case R.id.more: 
       goToUrl("http://www.example.com"); 
       break; 

    return super.onOptionsItemSelected(item); 
} 

私はそのあなたに便利な:) ハッピーコーディング:)

-1

これを試してみてください。

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      onBackPressed(); 
      return true; 
     case R.id.more: 
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, 

Uri.parse("http://www.google.com")); 

startActivity(browserIntent); 
      return true; 
      break; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
+0

あなたは何が違うか教えていただけますか?なぜあなたは真実を返してくれるでしょうか?それは実際にコンパイルエラーを与えませんか? – k0sh

関連する問題