error in adding google play services as reference library in eclipse - android

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.

Related

Android Studio creating new activity has no "Blank activity" choice

Each time I tried to create new phone project with AS and adding new activity (as show in the illu) I can not see "Blank activity" in the menu of items.
There is simply no Blank Activity. Android Studio change the Blank Activity into Basic Activity
Select java class and extend AppCompatActivity:
public class MainActivity extends AppCompatActivity {
// set here your layout by using setContentView
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Using activities within libraries in main application project

I am working on an Android project where I have a library project which has an activity.
I have a main app project which references the library project and tries to start the activity within the library project. In the library project I have the following activity:
public class DirectoryPicker extends Activity
{
GridView gridView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directory_picker);
gridView = (GridView)findViewById(R.id.directory_picker_gridview);
DirectoryAdapter.directories.add("String1");
DirectoryAdapter.directories.add("String2");
DirectoryAdapter.directories.add("String3");
DirectoryAdapter.directories.add("String4");
gridView.setAdapter(new DirectoryAdapter());
}
}
I am trying to start the activity using the following in my main application project.
protected OnPreferenceClickListener mPrefDefaultBackupClickListener = new OnPreferenceClickListener()
{
#Override
public boolean onPreferenceClick(Preference preference)
{
Intent intent = new Intent(getActivity(), DirectoryPicker.class);
startActivity(intent);
return false;
}
};
In my main application project AndroidManifest file I've added the following:
<activity android:name="com.MyCompany.Library.DirectoryPicker">
</activity>
When I try and launch the activity I get the following exception:
java.lang.NoClassDefFoundError: com.MyCompany.Library.DirectoryPicker
I can't see any reason why it doesn't work.
I've managed to find out the problem finally. Didn't manage to find any documentation but just trial and error and found if I go to File > Project Structure > Depdencies and then select my library and change the scope from compile to Provided and clean and rebuild everything the activity is then launched correctly.

R class not updating

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

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?

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