I'm trying to make a simple RSS feed reader app. I've created a class file with the name "IotdHandler.java" and a public class with the same name. When I try and create a new instance of the class I get the following error: "Unreachable code".
If anyone can point me in the direction of a good tutorial about working with multiple class files I would be very grateful.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nasa_daily_image, menu);
return true;
IotdHandler handler = new IotdHandler();
handler.processFeed();
}
Thats because you have a return statement before.
Unreachable code has nothing to do with the class/library, is a compiling error because there's no way the code below the "return true" will be executed in anyway, you should get a good Java Book before diving into Android.
Regards!
Related
So I have been working with Eclipse and Android Application projects for a few months now, and recently, I have had some problems.
So to give the full backgroud, I changed the target SDK and API of a project, and downloaded every SDK package available. Since then, I have had numerous problems with my programs. But now, everytime I created a new Android Application Project, everything that is auto-generated by Eclipse has a red squiglly line under it and an error message stating that the method "must override or implement a supertype method" or that the "method is undefined" or "cannot be resolved to a type".
I have attempted a Clean and have restarted. I changed the SDK level and the API.
![this is a shot of my code.][1]
Here is my code from my Main Activity file.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
These are the errors that show up on the bottom of Eclipse in the console.
*Found 2 versions of android-support-v4.jar in the dependency list,
but not all the versions are identical (check is based on SHA-1 only at this time).
*All versions of the libraries must be the same at this time.
*Jar mismatch! Fix your dependencies
I'm not sure what to do, and apologize for providing so much information. Any help would be greatly appreciated!
UPDATE: Now, when I create a new Android Application Project, there is nothing in the src file. I really have no idea what the heck is going on.
This happens when you have added the support jars to your main project and also to library projects. They all come bundled together. Make sure any of your library projects don't have the support jars added to them as libs. If they have and u need them to be there then remove those library jars associations from your main project.
I had the same kind of problem. They way I fixed it was that I used only the latest version of SDK. I had to get rid of all the other SDK packages.
Hello Android Developers,
I have seen a lot of question regarding the update of Options Menu. However, every answer says I have to call invalidateOptionsMenu().
My question is, is there any other way of updating the Options Menu without invoking the method invalidateOptionsMenu()?
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); // Clear the menu first
/* Add the menu items */
return super.onPrepareOptionsMenu(menu);
}
This solved the problem on updating the OptionsMenu without invoking the invalidateOptionsMenu()
I'm wondering if it would be possible to tell android to split the ActionBar only when I want it to, but on the same Activity. My use case is that by default, the actions I have on the bar are OK to be collapsed, but on a long click on an item, I enter an "Edit Task" mode, where the action bar is used to provide some shorthands to edit a task. I'd like this "edit mode" to use the split action bar, as it has icon's that are better off to be visible right away, and keep the "not split" action bar for the general view - where it's just "settings" etc.
So the question is, can I set android:uiOptions="splitActionBarWhenNarrow" from the code, instead of hardcode it in the Manifest?
PS: I'm using ActionBar Sherlock.
The native action bar can be set into split mode by calling getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW).
Window UI options cannot be read after they are set so with ActionBarSherlock you have to call getSherlock().setUiOptions(...). You don't have to call both. ABS will automatically call the above when appropriate.
This must be done before the decor view has been created. The safest place to put this call to ensure that always happens is in your activity onCreate method before you call super.onCreate.
Take a look at the ActionBarSherlockSamples, SplitActionModes.java. In this example, when the button 'Start' is pressed, a split action bar shows up in the bottom of the screen:
Call this to show the split actionbar:
mMode = startActionMode(new AnActionModeOfEpicProportions());
mMode is type of ActionMode and you need to call 'finish()' on it when you want the action bar to go away.
AnActionModeOfEpicPropotions is an implementation of ActionMode.Callback:
private final class AnActionModeOfEpicProportions implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// add your menu here...
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// handles your action menu clicked event
return true;
}
}
I was just trying if I could have an ActionBar in one Activity and a Split one in another.
I added the action bar in onCreateOptionsMenu in both the activities and added
getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
right before onCreate in the activity I wanted the split action and to my surprise it worked. :)
Then I used getActionBar().hide() to hide the split action bar on button click and it worked too.
I guess if you could try this, you can get this working and customized to your need. Hiding it initially and onLongClick showing it up and after the necessary actions are performed hiding it again.
(The only thing I missed is checking this with support libraries. Will do and update you)
Hope, this might help you in some way. Happy coding :)
private final class AnActionModeOfEpicProportions implements ActionMode.Callback
{
#Override
public boolean onCreateActionMode(ActionMode mode,Menu menu)
{
// add your menu here...
}
#Override
public boolean onActionItemClicked(ActionMode mode,MenuItem item)
{
// handles your action menu clicked event
returntrue;
}
}
I am using ActionBarSherlock and have implemented ShareActionProvider.
When a user selects an app to share content with, eg Twitter, the actionbar displays a Twitter icon next to the Share button icon. This stays there forever.
Does anybody know how to disable the application icon from appearing next to the Share button?
Found the answer:
Implement OnShareTargetSelectedListener and set it on the
ShareActionProvider
public void onCreateOptionsMenu(Menu menu){
....
actionProvider.setOnShareTargetSelectedListener(this);
....
#Override
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
context.startActivity(intent);
// started activity ourself to prevent search history
return true;
}
The top target is featured in the action bar. This is the behavior of the widget as it exists in Android.
If you do not want this behavior copy the sources into your app and modify its behavior to never display the top target icon.
In my little app users can have a look at 'more information' or send me feedback when they click on the menu button (of the device) at every xml file of the app.
This is my code (when you click on the menu button):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.impressum:
startActivity(new Intent (this, MoreInformation.class));
break;
case R.id.feedback:
/*
*
*/
break;
}
return super.onOptionsItemSelected(item);
}
Now I put the code into all java classes. This obviously works but I'd like to create a class (Let's call it isMenuButtonPressed.class) which contains the code above and I'd like to remove the code above from all other java classes and just call isMenuButtonPressed.class (in these java classes when the menu button was pressed). How can you do that? How do you pass the information on that the menu button was pressed? How do you receive it in the isMenuButtonPressed.class?
Thanks!
How do you write an extra class for often used code and call it from everywhere?
By letting a custom class define the actual behaviour for your Menu.
Read my Asynchronous programming best practices answer for more information regarding this subject.