3

I'm trying to add a map in a project but I cannot because there is some error when I debug the application with the fragment.

This is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.MapFragment"/>

This is my .java file:

   import android.os.Bundle;
   import android.app.Activity;

   public class MainActivity extends Activity {

   @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      }
   }

And, This is my Manifest:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inkadroid.mapa"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >



    <activity
        android:name = ".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="My_KEY"/>

</application>

</manifest>

Also, I've added google_play_services library.

Visit http://i61.tinypic.com/2jdjaky.jpg

And, I get this error. If anyone know how to solve it, please tell me.

06-02 22:21:50.615: E/AndroidRuntime(869): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.inkadroid.mapa/com.inkadroid.mapa.MainActivity}: 
android.view.InflateException:     Binary XML file line #2: Error inflating class fragment
06-02 22:21:50.615: E/AndroidRuntime(869):  at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-02 22:21:50.615: E/AndroidRuntime(869):  at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
4
  • Did you try dumping the Fragment inside a ViewGroup? Commented Jun 3, 2014 at 3:40
  • post your activity_main xml Commented Jun 3, 2014 at 3:40
  • 1
    your activity should extend fragmentactivity i guess or actionbaractivity Commented Jun 3, 2014 at 3:40
  • can you post the entire LogCat exception? towards the bottom of the exception log is usually an indicator of why the XML couldn't be inflated (ie: NPE etc) Commented Jun 3, 2014 at 3:44

3 Answers 3

3

First you need to move <meta-data> tag under <application> tag in your manifest.xml.

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

and your minsdk="8" so you should change this

public class MainActivity extends Activity

to

public class MainActivity extends FragmentActivity

and also change this

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.MapFragment"/>

to

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>
Sign up to request clarification or add additional context in comments.

Comments

0

Your meta tag should be inside application tag. This one:

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

5 Comments

it looks like the OP does have the meta-data within the application tag
but his googleplayservercies version is not inside application tag according to what he posted also he is setting min sdk version to 8 so he should extend fragmentactivity thats what I did in my project
ah whoops, I was looking at the wrong meta-data block
Also OP needs to replace the API key value with his key generated by the google API console.
@android_Muncher you dont usually post your api key publicaly because you have to post your package name and with the api key you can do a lot of bad works so thats fine
0

Try to change your xml like:

 <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

and edit your main activity to extend FragmentActivity like this:

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.