2015-07-10 78 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

问题已解决,谢谢大家的帮助 –