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);
}
}
Related
in the code shown below how can i return boolean values from this observable (lambda expression)
loginActivityViewModel.checkEmailAndPassword(email,password).observe(this,(response)->{
switch(response){
case LoginActivityViewModel.EMPTY_EMAIL:
handleError(emailWrapper, R.string.error_email_required);
return false;
case LoginActivityViewModel.INVALID_EMAIL:
handleError(emailWrapper, R.string.error_enter_valid_email);
return false;
case LoginActivityViewModel.EMPTY_PASSWORD:
handleError(passwordWrapper, R.string.error_password_required);
return false;
}
});
this block of statement is inside an function that returns boolean values but IDE is telling me unexpected return statement inside the cases.
Thankyou for helping in advance
You are trying to return from within the observable function, not the function that contains your code block. This does not work. Assuming the call on checkEmailAndPassword is the whole point of your method, a better option is to pass a callback function to your method instead of returning a boolean.
You missing default:
loginActivityViewModel.checkEmailAndPassword(email,password).observe(this,(response)->{
switch(response){
case LoginActivityViewModel.EMPTY_EMAIL:
handleError(emailWrapper, R.string.error_email_required);
return false;
case LoginActivityViewModel.INVALID_EMAIL:
handleError(emailWrapper, R.string.error_enter_valid_email);
return false;
case LoginActivityViewModel.EMPTY_PASSWORD:
handleError(passwordWrapper, R.string.error_password_required);
return false;
default:
return false;
}
});
I don't understand what happens when I don't return super.onCreateItemSelected(item). Does android take it by default?
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.action_search) {
Context context = MainActivity.this;
String textToShow = "Search clicked";
Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
return true;
}
}
This code ran fine. But everywhere I see use to super.onCreateItemSelected(item).
The super class may need to handle occurrences in the action/tool bar.
So it is essential to call the super classes onOptionsItemSelected.
I have a this code somewhere in my Android project:
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
I get a lint warning at the second if statement: 'if' statement could be simplified. That's obviously because I could write as well:
return publicLoad && publicLoadInProgress;
However, I would like to keep it this way for readability. I know that there is some inline comment annotation for switching off the lint warning at that place, but I can't find it in the Android Lint documentation. Can you tell me what this annotation/comment was?
The simple code comment for disabling the warning is:
//noinspection SimplifiableIfStatement
This on top of the if-statement should switch off the warning only at that place.
In the example, this would be:
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
//noinspection SimplifiableIfStatement
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
You can add #SuppressWarnings("SimplifiableIfStatement") above your method.
It's not an Android Lint error. You can use:
#SuppressWarnings("RedundantIfStatement")
public static boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
At the highlighted if, you can use the alt-enter shortcut to open the context menu and select Simplify > Suppress for method (keeping the scope as small as possible).
Sure:
In .java files, you can suppress issues with the #SuppressLint
annotations. You supply the lint issue id as the argument to the
annotations.
Example:
#SuppressLint("AndroidWarningId")
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
Just replace the AndroidWarningId with the corresponding warning, you can find those in here
Although I would suggest simplifying it this way:
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress
|| publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
Its still readable and uses less space (kind of ugly though, but better than a supresslint).
You can also suppress more than one issue using a comma separated list:
#SuppressLint({"NewApi","StringFormatInvalid"})
Cheers!
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.
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