2015-10-04 36 views
0

我是Android Studio的新手。我正在學習通過developer.android.com進行編程。我正在添加操作欄,但現在我正面臨一個錯誤。在MainyActivity.java中設置搜索和設置代碼時出錯

` 

@Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle presses on the action bar items 
      switch (item.getItemId()) { 
       case R.id.action_search: 
        private void openSearch() { 
        Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show(); 
       } 


        return true; 
       case R.id.action_settings: 
        private void openSettings() { 
        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0); 
       } 
        return true; 
       default: 
        return super.onOptionsItemSelected(item); 
      } 
     } 

這裏是我的代碼,openSearch()openSettings()後,我得到錯誤添加「;」`但是當我添加它,它再次顯示了表達的預期。請儘快幫助我。提前致謝!

回答

0

這是因爲您正在開關盒內創建一個方法。

拉法出來,只是用它裏面,像這樣:

private void openSearch() { 
    Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show(); 
} 

private void openSettings() { 
    startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0); 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle presses on the action bar items 
    switch (item.getItemId()) { 
     case R.id.action_search: 
      openSearch(); 
      return true; 
     case R.id.action_settings: 
      openSettings(); 
     return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
+0

謝謝JozeRi,即工作!非常感謝。 :) –

+0

高興幫忙:)快樂編碼! – JozeRi