R class not updating - android

When I create a new activity All the code is generated. But R.java class is not updated.
Please can anyone come to my rescue. I am about to finish this project. Its the error near Activity_maininfo.
public class Maininfo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maininfo);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.maininfo, menu);
return true;
}
}

One of the following should work
Project -> Clean,
Right click -> Fix Project Properties
Check all of your XML
Maybe try restarting Eclipse

Related

error in adding google play services as reference library in eclipse

1> When i add google play services library it shows green right mark but when i press ok and reopen project properties its showing red cross mark.
2> In eclipse when i create new android project it by default creating activity which ActionBarActivity insted of Activity. Why?
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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;
}
I got the solution
*copy google-play-services-lib and paste it in other location.
*change eclipse workspace.
* Importe google-play-services-lib as Existing Android Code Into Workspace. browse to new location and select google-play-services-lib folder.
* Goto project properties->android->Add and select google-play-services-lib.

Android: Title of the action bar disappears

so, in my main activity, on the onCreate() method, I check if it is the app first run with shared preferences... If it is the first run of the app, the user is redirected to a welcome activity, and then, when I press the back button and return to the main activity, the title in the action doesn't show up.. I have tested with api 9 and 17, and this only happens with api 9, so I'm guessing the error must be something about using the support library for the action bar .. Can someone help me ?
Main:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
SessionManager session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// session
session = new SessionManager(getApplicationContext());
// check first time app run
session.checkFirstRun();
}
#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;
}
}
SessionManager First Run check method
// check first run
public void checkFirstRun() {
if(getFirstRunStatus() == true) {
// set first run key as false
editor.putBoolean("FIRST_RUN", false);
editor.commit();
// first time running the app, redirect user to welcome activity
Intent i = new Intent(_context, WelcomeActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
public boolean getFirstRunStatus() {
return pref.getBoolean(FIRST_RUN, true);
}
Add the title in a couple ways:
XML:
<activity
android:name=".....WelcomeActivity"
android:icon="#drawable/logo"
android:label="#string/app_name"
</activity>
On the Fly:
.setTitle("TITLE");
.setIcon(R.drawable.logo);
You can pass the title in an intent to if you want it to be dynamic... not sure if that is what you want:
.setTitle(extras.getString("title"));
Hope that helps.
try this in your onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu_actions, menu);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("title");
actionBar .setDisplayShowTitleEnabled(true);
// OR:
// getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
return true;
}

There was protected in a file created automatically what was private

When I make a new Android Application Project
I see the automatic generate MainActivity.java file.
Why was the "OnCreate" protected
I remember this was private. I don't understand. why made this?
For example this:
public class MainActivity extends Activity {
#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;
}
}
It never was private, if it was private then how you were able to Override it?? You might have seen it public because subclass can not reduce the visibility of a method. Read this question for clarification. Why can't you reduce the visibility of a method in a Java subclass?

Android Memory Leak? Bitmap exceeds VM budget error with MapView

I've written an application that creates a map activity. From there the user can switch to a menu and goes back to the map activity. After about 10 of those loops, the following error occurs:
02-28 21:35:54.780: E/AndroidRuntime(23502): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
I've tried the unbind drawables solution proposed here http://www.alonsoruibal.com/bitmap-size-exceeds-vm-budget/ and in various other threads but that did not help.
The only thing that helps is closing the map activity manually via finish(), but that causes an unnatural navigation behavior.
Here is my code:
MapActivity class
public class TestMapsForgeActivity extends MapActivity {
View mapView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = new MapView(this);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
return true;
};
#Override
public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(getApplicationContext(), MenuActivity.class));
return true;
}
}
MenuActivity Class
public class MenuActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
};
#Override
public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(getApplicationContext(), TestMapsForgeActivity.class));
return true;
}
}
What I don't understand is, the garbage collector does apparently not destroy the MapActivity properly unless I close it with finish(). But shouldn't android call finish() by itself as soon as the application needs more memory?
Does anyone have some thoughts on this issue?
Thanks in advance!
I think the problem is that you are starting an activity over another that wasn't closed.
Try this:
Intent i = new Intent(getApplicationContext(), TestMapsForgeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Setting the Intent flag CLEAR_TOP, will finish the others previous activitys, read more here: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

startActivity crashes when trying to respond to menu click

I know this has been covered elsewhere, but I'm new to the Android platform and am having a hard time figuring out how to add basic menu options to my first app.
I have an options menu setup using
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
and
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuPrefs" android:icon="#android:drawable/ic_menu_preferences" android:title="Settings"></item>
</menu>
Then within my main java class I have
#Override
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.menuPrefs) {
showPrefs();
}
private void showPrefs() {
Intent i = new Intent(this, Prefs.class);
startActivity(i);
}
And then in Prefs.java I have
public class Prefs extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Toast.makeText(getBaseContext(), "FNORD", Toast.LENGTH_LONG).show();
}
}
Now from this I would expect to see the Toast message "FNORD" when the menu option is pressed, however the application stops unexpectedly.
If I move the toast statement into the showPrefs() function in place of the startActivity call it works.
You forgot to call super.onCreate(Bundle savedInstanceState); on Prefs' onCreate() method.
You need to learn how to Debug in Eclipse and how to use the ADB and DDMS tools.
In order to get more details about an exception/force close you need to look for a view in Eclipse called Logcat(you will find in the DDMS perspective) there you will find a detailed traceback when/what and on what line is the issue.
For this you should read a complete article about Debugging in Android using Eclipse
(source: droidnova.com)
Solution was that I needed to add the Prefs.xml to the manifest.

Categories

Resources