Reputation: 540
I am try to develop a project on Google Map API in android. I am using Eclipse version of JUNO, SDK level-17.
The problem is, when the application is start loading, it is showing an unfortunate error. Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.googlemap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.googlemap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.googlemap.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxx...xxx"/>
</application>
</manifest>
mainActivity.java
package com.example.googlemap;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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;
}
}
I am getting the error message like below:
android.view.InflateException: Binary XML file line #1: Error inflating class fragment
Do me another favor plz... Why does google API library is not adding into my project?
Any help please...
Upvotes: 0
Views: 2102
Reputation: 133580
Your class must extend FragmentActivtiy
Make sure you import
import android.support.v4.app.FragmentActivity;
Since you rmin sdk is 8 you should use SupportMapFrament.
You can also check this as a side note.
Activity Layout: Fragment class: vs android:name attributes
This is the later part of initializing GoogleMap object and using the same.
To initialize Google Map Object
You should use getSupportFragmentManager()
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap();
Import
import com.google.android.gms.maps.SupportMapFragment;
Edit:
You must Choose the Google API's instead of Android 4.2.2.
Also make sure you refer to the google library project properly. Looks like it's a broken link from the picture posted.
Copy the google-play services_lib library project to your workspace (folder where your map project is). The library project can be found under the following path.
<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib library project .
Import the library project into eclipse
Click File > Import, select Android > Existing Android Code into Workspace, and browse the workspace import the library project. You can check if it is library project. Right click on the library project. Goto properties. Click Android on the left panel. You will see Is Library checked.
You need to refer the google-play services_lib library project in your android project.
Right click on your android project. Goto properties. Choose Android on the left panel. Click on Add and browse the library project. Select the same. Click ok and apply.
Upvotes: 2
Reputation: 6899
In class file you are using Fragment.Use SupportMapFragment
import com.google.android.gms.maps.SupportMapFragment;
GoogleMap gm;
gm = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
gm.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Upvotes: 1
Reputation: 2732
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Use class
instead of android:name
Upvotes: 1