2

I want to start an Intent with a Google Maps fragment. The sample code of Google Play Services works fine but I don't get these maps running in my application. My referenced libraries are:

  • android-support-v4.jar
  • google-play-services_lib.jar
  • google-play-services.jar
  • The Android jar is the target 4.2 also platform android-17

I get this errors:

java.lang.RuntimeException: Unable to start activity \
       ComponentInfo{app.cookingplace/app.cookingplace.MapsActivity}: \
       android.view.InflateException: Binary XML file line #8: \
       Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)

Caused by: android.view.InflateException: Binary XML file line #8: \
       Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
    at android.app.Activity.setContentView(Activity.java:1657)
    at app.cookingplace.MapsActivity.onCreate(MapsActivity.java:12)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more

Caused by: android.support.v4.app.Fragment$InstantiationException: \
       Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: \
       make sure class name exists, is public, and has an empty constructor that is public
    at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
    ... 20 more

Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment in loader \
       dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:\
       /data/app/app.cookingplace-2.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
    ... 23 more

My Code:

LoginActivity.java

package app.cookingplace;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity implements OnClickListener {

    // All static variables
    static final String baseURL = "http://sfsuswe.com/~f12g22/web/php/Login.php?";
    String URL = "";
    // XML node keys
    static final String KEY_SESSION = "session"; // parent node
    static final String KEY_ACTIVATED = "activate";
    String activated = "";
    static final String KEY_REGISTERED = "registered";
    String registered = "";
    static final String KEY_USERID = "userId";
    String userId = "";
    static final String KEY_LOGONNAME = "logonName";
    String logonName = "";

    private Button login_button;

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

        login_button = (Button) findViewById(R.id.login_button);
        login_button.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        EditText nameLogin = (EditText) findViewById(R.id.user_logon_id);
        String loginName = nameLogin.getText().toString();
        EditText passwordLogin = (EditText) findViewById(R.id.password_logon_id);
        String password = passwordLogin.getText().toString();

        String parameters = "logon=" + loginName + "&password=" + password;
        URL = baseURL + parameters;

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SESSION);

        Element e = (Element) nl.item(0);
        // adding each child node to HashMap key => value
        activated = parser.getValue(e, KEY_ACTIVATED);
        registered = parser.getValue(e, KEY_REGISTERED);
        userId = parser.getValue(e, KEY_USERID);
        logonName = parser.getValue(e, KEY_LOGONNAME);

        if (registered.equals("1")) {
        Intent nextScreen = new Intent(LoginActivity.this,
                MapsActivity.class);
        startActivityForResult(nextScreen, 0);
        }
    }
}

MapsActitity.java

     package app.cookingplace;

     import android.os.Bundle;
     import android.support.v4.app.FragmentActivity;
     import android.view.Menu;

     public class MapsActivity extends FragmentActivity {

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

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_maps, menu);
                return true;
            }
        }

activity_login.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity" >

<EditText
    android:id="@+id/user_logon_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="@string/user_login" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/password_logon_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/user_logon_id"
    android:ems="10"
    android:inputType="textPassword"
    android:text="@string/password_login" />

<Button
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/password_logon_id"
    android:text="@string/button_login" />

activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" >

    <fragment
        class="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
/>

</RelativeLayout>

AndroidManifest.xml

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

    <permission
        android:name="app.cookingplace.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="app.cookingplace.permission.MAPS_RECEIVE" />
    <!-- Copied from Google Maps Library/AndroidManifest.xml. -->
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!-- External storage for caching. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- My Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!-- End of copy. -->

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="-My key tested and works fine-" />

        <activity
            android:name="app.cookingplace.LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="app.cookingplace.MapActivity"
            android:label="@string/title_activity_map" >
        </activity>
        <activity
            android:name="app.cookingplace.TestActivity"
            android:label="@string/title_activity_test" >
        </activity>
        <activity
            android:name="app.cookingplace.MapsActivity"
            android:label="@string/title_activity_maps" >
            </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>

</manifest>
JJD
  • 50,076
  • 60
  • 203
  • 339
King Luy
  • 141
  • 3
  • 15
  • you have classnotfound Error have you added up all the classes in manifest file? – Usman Kurd Dec 24 '12 at 08:16
  • i think i have done it bu i have sean the classnotfound Error. ich have included the same jars like in the sample project and it works. that is my problem. i cant find the missing components. – King Luy Dec 24 '12 at 08:45

6 Answers6

4

In MapsActitity.java Add this import ( import android.support.v4.app.FragmentActivity; )

Change

 public class MapsActivity extends Activity

To

public class MapsActivity extends FragmentActivity

Read this http://developer.android.com/training/basics/fragments/creating.html

Rahul
  • 10,457
  • 4
  • 35
  • 55
  • When i use public class MapsActivity extends FragmentActivity i get this error: 12-24 11:04:52.046: E/dalvikvm(3649): Could not find class 'app.cookingplace.MapsActivity', referenced from method app.cookingplace.LoginActivity.onClick – King Luy Dec 24 '12 at 10:05
  • Add in your manifest, between and – Rahul Dec 24 '12 at 10:21
  • done but i get errors again 12-24 11:37:24.296: E/AndroidRuntime(5762): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.cookingplace/app.cookingplace.MapsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment – King Luy Dec 24 '12 at 10:38
  • my current activity_maps.xml looks like – King Luy Dec 24 '12 at 10:52
  • King Luy Try this also. You shouldn't be transitioning the views, but actually starting a new activity. Intent nextScreen = new Intent(LoginActivity.this,MapsActivity.class); startActivityForResult(nextScreen, 0); – Rahul Dec 24 '12 at 10:54
  • stil fail 12-24 11:59:15.109: E/AndroidRuntime(6962): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.cookingplace/app.cookingplace.MapsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 12-24 11:59:15.109: E/AndroidRuntime(6962): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment – King Luy Dec 24 '12 at 11:02
  • 12-24 11:59:15.109: E/AndroidRuntime(6962): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/app.cookingplace-2.apk] – King Luy Dec 24 '12 at 11:02
  • Read the answer of this http://stackoverflow.com/questions/13696620/google-maps-android-api-v2-authorization-failure. – Rahul Dec 24 '12 at 11:09
  • Update your manifest file in your question. Did you put the in your manifest, between and – Rahul Dec 24 '12 at 11:19
2

Just change this thing in your MapsActitity.java file public class MapsActivity extends android.support.v4.app.FragmentActivity . You need to extends the FragmentActivity not simple activity .

This is the issue where your application is trying to get the FragmentActivity which is not you have extended.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • When i use public class MapsActivity extends android.support.v4.app.FragmentActivity i get this error:12-24 11:02:57.039: E/dalvikvm(3553): Could not find class 'app.cookingplace.MapsActivity', referenced from method app.cookingplace.LoginActivity.onClick – King Luy Dec 24 '12 at 10:04
  • Just define only class name in manifest file like this `` and try out. Now i hope it should work. – GrIsHu Dec 24 '12 at 11:30
1

Now it works fine. I followed that tutorial my lib project was marked with a red X that was the point. Follow this tutorial to fix it.
The solution is a combination of all your hints. Thanks a lot. Guys you have saved my Xmas.

JJD
  • 50,076
  • 60
  • 203
  • 339
King Luy
  • 141
  • 3
  • 15
1

This is a working example do have a look it worked for me Here's a link!

Your xml include class="com.google.android.gms.maps.SupportMapFragment" inside the fragment tag

extend your activity from FragmentActivity

and if you want to make a map object use this

SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

Code is here as follows: XML

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment"/>

MainActivity

 public class MainActivity extends FragmentActivity  {


     // Google Map
private GoogleMap googleMap;

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


 .......

     SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
    ....
Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
0

On witch Android version are you trying? If the version is Pre-Honeycomb, (<3.0) then you can't use fragments, like indicates in docs

You should use SupportMapFragment like indicates here, and ensure that the libs are included on path project.

Hope it helps.

sabadow
  • 5,095
  • 3
  • 34
  • 51
  • in activity_maps.xml is the class definition class="com.google.android.gms.maps.SupportMapFragment" is that not that what you mean? – King Luy Dec 24 '12 at 08:50
  • that and that your Activity `MapsActivity` should extends from `android.support.v4.app.FragmentActivity` to use `SupportFragments` before Android 3.0 (and maybe some other fixes like this) – sabadow Dec 24 '12 at 09:19
  • with that i get this errors: 12-24 11:05:59.828: E/dalvikvm(3820): Could not find class 'app.cookingplace.MapsActivity', referenced from method app.cookingplace.LoginActivity.onClick – King Luy Dec 24 '12 at 10:06
  • Have your project a `libs` folder where you add the needed jars? which version of SDK tool are using? chek this http://stackoverflow.com/questions/9931040/fragmentactivity-causing-classnotfoundexception and http://stackoverflow.com/questions/5502907/could-not-find-class-exception-android – sabadow Dec 24 '12 at 10:16
  • My workspace looks like that http://s7.directupload.net/file/d/3114/7pwm253c_png.htm I have downloaded yesterday the SDK tool. So i thinke it is up to date. The Sample of the google play services works fine in that device. i copyed the cood snippeds from there. – King Luy Dec 24 '12 at 10:34
  • the SDK Manager Version is 21.0.1 – King Luy Dec 24 '12 at 10:42
0

Are you sure the target AVD, where you are trying to run your project is version 11+ , because fragments are not allowed below 11.

Rahul
  • 67
  • 2
  • 8