0

i am newcomer on stackoverflow and i have google map test project that use from here and i do just like tutorial but apps crashes.

i done every single steps in tutorial and get android key, added library and more: my similar problem is this link: here but it don't have any answer that works. i completly delete eclips, uninstall java JDK and JRE and even my whole system java. but the app is crashes. can any body help me on this? thank you very much

here my code:

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;



public class MainActivity extends FragmentActivity {

     private GoogleMap googleMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void initilizeMap() {
        if (googleMap == null) {

            //googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            googleMap =   ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
}

and manifest file is:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.maptest"
    android:versionCode="1"
    android:versionName="1.0" >


     <permission
        android:name="com.test.maptest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.test.maptest.permission.MAPS_RECEIVE" />

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

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

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

            <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBYju6h2BWvZOaSDQpe5f9tv6fJsZy6cY8" />

</manifest>

and layout is:

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

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

</RelativeLayout>

and this is logcat:

11-25 14:04:50.663: E/Trace(1411): error opening trace file: No such file or directory (2)
11-25 14:04:50.853: D/AndroidRuntime(1411): Shutting down VM
11-25 14:04:50.853: W/dalvikvm(1411): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-25 14:04:50.873: E/AndroidRuntime(1411): FATAL EXCEPTION: main
11-25 14:04:50.873: E/AndroidRuntime(1411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ariagostar.maptest/com.ariagostar.maptest.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.os.Looper.loop(Looper.java:137)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at dalvik.system.NativeStart.main(Native Method)
11-25 14:04:50.873: E/AndroidRuntime(1411): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.Activity.setContentView(Activity.java:1867)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.ariagostar.maptest.MainActivity.onCreate(MainActivity.java:25)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.Activity.performCreate(Activity.java:5008)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-25 14:04:50.873: E/AndroidRuntime(1411):     ... 11 more
11-25 14:04:50.873: E/AndroidRuntime(1411): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
Community
  • 1
  • 1
Mahdi
  • 6,139
  • 9
  • 57
  • 109

3 Answers3

0

Change

public class MainActivity extends FragmentActivity {

to

public class MainActivity extends Activity {

Change this

googleMap =   ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

to

googleMap =   ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

You also need the below in application tag in manifest

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

which is a requirement of google play services rev 13

Edit:

Move

 <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBYju6h2BWvZOaSDQpe5f9tv6fJsZy6cY8" />

inside application tag in manifest

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • thank you. i done but still crashesh. app come up for while but it crashesh :( – Mahdi Nov 25 '13 at 12:40
  • @mahdit you need to post the stacktrace for further help – Raghunandan Nov 25 '13 at 12:41
  • how i can send you stacktraces? – Mahdi Nov 25 '13 at 12:45
  • @mahdit extract the lines that show the exception in logcat and post it here. – Raghunandan Nov 25 '13 at 12:46
  • i put Log.e("LOG", "I got an error", e); on catch of initilizeMap() try section. but nothings loged with LOG filter. – Mahdi Nov 25 '13 at 12:49
  • @mahdit you will see a caused by part in your logcat which indicates the cause of exception. post it here. – Raghunandan Nov 25 '13 at 12:51
  • @mahdit remove all the imports from support library and make the changes suggested in my post clean and build project. You need to use `MapFragment` extend `Activity` – Raghunandan Nov 25 '13 at 12:57
  • @mahdit check line 25 `MainActivity.java`. What is it? – Raghunandan Nov 25 '13 at 13:01
  • it's refer on setContentView(R.layout.activity_main); that in MainActivity. – Mahdi Nov 25 '13 at 13:03
  • @mahdit did you change `googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();`?? and did you extend `Activity`. Also move the meta tag inside application in manifest – Raghunandan Nov 25 '13 at 13:05
  • thanks, the crash was gone with your two tips. but app said that i must upgrade my google play service on Glaxy s 3. so what i can do to run the app even on older androids when it's wont even run on my 4.0.1 phone :( – Mahdi Nov 25 '13 at 13:15
  • @mahdit update google play services. google maps api is part of google play services so you will need it to show maps. also for api level 11 and below you will need to use `SupportMapFramgent` and extend FragmentActivtiy. In your manifest its 16 so no need to worry – Raghunandan Nov 25 '13 at 13:18
  • ok. i appreciation :). and one more question. how did you find out the problem from line25? – Mahdi Nov 25 '13 at 13:28
  • @Kenji usually the logcat indicates the caused by part and the line that causes the exception. you can look at it and find out the problem – Raghunandan Nov 25 '13 at 14:29
  • @Raghunadan i'm tring to use this app on skd 8 but app crashesh and make me crazy! what is should do? – Mahdi Dec 10 '13 at 06:44
  • @Kenji you need to use `SupportMapFragment`. Activity Need's to extend `FragmentActivty`. Should us Support library fro backward compatibility. With all this in place should work without a fuss. – Raghunandan Dec 10 '13 at 07:26
0

Try with this.

Your MainActivity

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        // Valida si el mapa no se creo correctamente
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(), "Sorry! el mapa no pudo ser cargado", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

In your layout this

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

In the manifest, i used this lines in MAC, because in Windows works correctly works without them

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Crash
  • 338
  • 1
  • 2
  • 12
  • my problem is solved and i think it was because of dont let metadata inside of tag. thanks for noticing ;) – Mahdi Nov 25 '13 at 13:32
0

I had a similar problemt. For me adding:

import com.google.android.gms.maps.SupportMapFragment;

And changing the xml to

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

resolved the problem. I hope you resolve it too!