Android Development: Undefined Method - android

Hi I´m new to Android and Eclipse. I have just following the tutorial from developer.android.com. Right now I´m in adding ActionBar
Right now I´m at this part
#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);
}
}
I have received an error for openSearch() and openSettings(). It said that The method openSettings() is undefined for the type DisplayMessageActivity. What shoud I do now?
Thanks

openSearch() and openSettings() are methods that the author of the tutorial created in order to perform other operations. Search well into the code, there must be somewhere the declaration of those methods, if the author made them visible.
They should look something like this:
public void openSearch() {
//Do something here.
}
public void openSettings() {
//Do something here.
}
Replacing the //Do something here with the code implementation present in the tutorial.

Im up to the same section as you, they haven't provided the methods but you have to implement them as stated above.
However I found code to open up the device settings using this code in the switch;
case R.id.action_settings:
startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
return true;

define them.
You're basing your code on an incomplete snippet. That snippet makes no expectation of what it means to search or create settings in your app... that's your job to implement. This snippet is only concerned about showing you how to establish the action bar, not the whole application.

The methods openSearch() and openSettings() should be defined. Use the following code. It'd help..
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.action_search :
startActivity(new Intent(Settings.ACTION_SEARCH_SETTINGS));
return true;
case R.id.action_settings :
startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
return true;
default :
return super.onOptionsItemSelected(item);
}
}

Maybe you should code those methods?
private void openSearch(){
//your code here
}
private void openSettings(){
//your code here
}

Those two methods are just examples how selecting an option can start an action. The implementation was not provided because it was irrelevant to the example. Note that it is not a tutorial, but a single and un-compile-able example of how to add behavior to an options item.

Related

Android Action Bar Responding to users

I am having issues with some methods with my app in android. I'm trying to respond to a button pressed by a user. Here is the method:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onContextItemSelected(item);
}
}
I was looking at the docs provided from google about this and it says those methods should be called depending on the user's action. Am I missing something?
The error messages area:
Error:(42, 17) error: cannot find symbol method openSearch()
Error:(46, 17) error: cannot find symbol method openSettings()
Any help would be appreciated!
Thanks
You have not defined the methods openSettings() and openSearch() inside the Activity where you define onOptionsItemSelected.
The result of this is that the compiler will tell you that it cannot find symbol method openSearch() and cannot find symbol method openSettings()
You simply have to add the method declaration inside the Activity:
private void openSettings(){
//Execute relevant code
}
private void openSearch(){
//Execute relevant code
}
The above function doesn't get executed on Button pressed event. It is executed when user selected an item from menu.
At the moment, compiler doesn't know if such method signatures exist in the class. You would need to define the functions inside the class, then use them. I guess it will work fine.

android how to change from if-else to switch case

I have an application about pmt function. However there are so many conditions that need to be handled. Somehow the app will not work with having more than 12 if-else. I want to use switch case, but i still not really understand how to use switch case(been 1 and half month since my 1st try using eclipse).Any example will be highly appreciated.
here is my example code:
if(String1.toString().equals("condition1")){
//do something
if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
//do something else
}
}
else if(String1.toString().equals("condition2")){
//do something
if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
//do something else
}
}
if(String1.toString().equals("condition3")){
//do something
if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
//do something else
}
}
and still keep going....to handle all possibilities .I am wondering, How to do this in switch case . Or a better implementation if we have 3 times 3 conditions. For example a,b,c(suppose these three conditions can only be used once) and d,e,f and g,h,i then condition 1 is a,d,g ; condition 2 is a,d,h condition 3 is a,d,i ; condition 4 a,e,g........on so on
Note:Suppose that the API version is 8-11 (old android)
thanks
The answer is dependent on your target version of android. From KitKat and upwards (API Level 19+), Java 7's switch (String) is available. I'd also strongly suggest trying to group the subcases (condition n.x) into different methods. It just gets very unwieldly quickly, otherwise:
switch (String1.toString) {
case "condition1":
handleCase1(String2, String3);
break;
case "condition2":
handleCase2(String2, String3);
break;
}
If that still results in too complex code, you can try a lookup table together with a command pattern:
class ConditionKey {
final String String1;
final String String2;
final String String3;
public int hashCode(); // hash strings
public boolean equals(); // compare strings
}
interface ConditionCommand {
// use whatever arguments the operation needs, you can also
// add fields and initialize in the constructor
void perform(final ConditionKey key, /* [...] */);
}
Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
new ConditionKey("condition1", "condition1.1", "condition1.2"),
new ConditionCommand() {
void perform(final ConditionKey key) {
// perform actions that need to be done
}
}
);
And then instead of the if-else or switch-case:
[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);
since java 1.7 switch on string is supported.
you could annidate two switch:
switch(String1) {
case "condition1": {
switch(String2) {
case "condition1.1":
break;
// ... other cases
default:
break;
}
}
break;
// ... other cases
default break;
}

Crashlytics not being called?

Trying to call from static function? Its initialized because it calls from the onCreate of the activity. Wondering how crashlytics works.. does it require reference to some context that is somehow not present. Here is some code:
Calling from the activities menu override:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.explore:
ListFragment.injectNewList(ListActivity.this, Stuff.getRandOffset());
break;
default:
break;
}
return true;
}
Calling function is a static function within a fragment:
public static void injectNewList(FragmentActivity activity, Integer offset)
{
ListFragment fragment = (ListFragment) activity.getSupportFragmentManager()
.findFragmentByTag(BaseFragmentActivity.LIST_FRAGMENT_TAG);
if(fragment != null)
{
fragment.nextOffset = offset;
FFData.getInstance().clearList();
fragment.mListAdapter.notifyDataSetInvalidated();
fragment.loadItems();
}
else
{
Crashlytics.log(Log.ERROR, "Log this error", "bad stuff happened!");
}
}
The activity and fragment are fully running when the menu button is clicked. I also see that the code is run in the debugger. Running on genymotion(will try actual device), SDK 19, Nexus5
Make sure Crashlytics is initialized first by calling Crashlytics.start(this);
Crashlytics.log will message will be visible in your dashboard, associated with crash (Meaning if no crash/exception happens, log will not be sent...Crashlytics is a crash tracking service, if you need to track custom messages there are other tools for that).

Syntax error on token "else" delete this token

I can't for the life of me fix this. It is returning an error on else. I tried everything on all the other threads that had the same problem, but it didn't work.
#Override
public boolean onOptionsItemSelected(MenuItem item) { //this method is used for handling menu items' events
// Handle item selection
switch (item.getItemId()) {
case R.id.goBack:
if(myWebView.canGoBack()) {
myWebView.goBack();
}
return true;
else
return super.onOptionsItemSelected(item);{
}
}
}
Eclipse is complaining because the else statement does not follow an if statement -- there's a return true in between (which by the way prevents any code after it from being executed). Fixing your indentation and code formatting helps discover (and also prevent) simple mistakes like this.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.goBack:
if (myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
}
return super.onOptionsItemSelected(item);
}
The error is because in java (and most-likely any programming language that defines if...else blocks such as c#, c, c++, etc.) the else block (if present) must come right after an if block or an else if statement definition any other statement between the if and else is illegal. However, you have a return statement right before the else block which is illegal and useless because any code blocks after a return statement becomes "unreachable"
You can change your code to...
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.goBack:
if (myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
break;
default:
return super.onOptionsItemSelected(item);
}
}

Menu item IDs in an Android library project?

The Android app uses a library project to contain most of the app code, as there are two versions of the app built from the core source. Since an IntelliJ IDEA update (to v11) I'm getting this warning on each of the case statements below:
Resource IDs cannot be used in a switch statement in Android library modules
Here's the code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_one: // Build error here
// Do stuff
return true;
case R.id.menu_item_two: // Build error here
// Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
OK, so if I can't reference them via their ID, how DO I reference them?
Substitute the switch with an if/else if construct.
int id = item.getItemId();
if(id == R.id.menu_item_one) {
// ...
}
else if(id == R.id.menu_item_two) {
// ...
}
This is neccessary since ADT 14 because the final modifier was removed from id's in the R class.
See Non-constant Fields in Case Labels

Categories

Resources