2015-07-10 77 views
0

我正在與this tutorial,我卡住了。新的Android工作室和Java,卡住了一個錯誤,我無法修復

我得到openSettings();openSearch();一個錯誤的程序說

無法解決方法

誰能告訴我我做了什麼錯?

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == R.id.action_settings) { 
     openSetting(); 
     return true; 
    } else if (item.getItemId() == R.id.action_search) { 
     openSearch(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+1

請發佈你的類的完整代碼+異常的logcat – TheTanic

+1

[openSearch()在Android初學者應用中未定義的可能的重複](http://stackoverflow.com/questions/18727033/opensearch-in-android-beginners-app沒有定義) –

回答

0

如果我猜的話,我認爲你是從這裏下面的例子: https://github.com/arun-gupta/android-samples/blob/master/HelloWorldProject/HelloWorld/src/main/java/name/arungupta/android/helloworld/MainActivity.java

解決你的問題,你需要一個添加這兩種方法在你的類:

private void openSettings() { 
    Intent intent = new Intent(this, SettingsActivity.class); 
    intent.putExtra(SETTINGS_MESSAGE, "Settings"); 
    startActivity(intent); 
} 

private void openSearch() { 
    Intent intent = new Intent(this, SearchActivity.class); 
    intent.putExtra(SEARCH_MESSAGE, "Search Now"); 
    startActivity(intent); 
} 
+0

這意味着我需要爲搜索和設置打開新的活動吧? –

+0

是的,您還需要定義這些類。只有當用戶點擊這些選項時纔會調用這些活動。我的回覆中有鏈接定義了這些類 – virtas

+0

問題已解決,謝謝大家的幫助 –