0

I am a beginner to android ... i am creating an app to load Google map v2.But it is not loading and keeps crashing every time with a message Unfortunatelyhas closed.Please help me to resolve the error..Thanks in advance..I am a regular follower stack overflow..

**my code**
**Main activity.java**
package com.mypack.test;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;

public class MainActivity extends Activity {

// Google Map
private GoogleMap googleMap;

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

    try {
        // Loading map
        initilizeMap();

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

}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().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();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

}

Test manifest

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

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

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

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

  <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:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mypack.test.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>
    <!-- Goolge API Key -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyDnHxpS7Pb6YeKBmyWjV_Slj9mIjo6M1FM" />
</application>

</manifest>

activity_main.xml

<?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>

Logcat

 09-19 09:23:08.785: E/Trace(803): error opening trace file: No such file or  directory  (2)
 09-19 09:23:09.515: W/dalvikvm(803): VFY: unable to resolve static field 1065   (MapAttrs) in Lcom/google/android/gms/R$styleable;
 09-19 09:23:09.515: D/dalvikvm(803): VFY: replacing opcode 0x62 at 0x000e
 09-19 09:23:09.525: D/AndroidRuntime(803): Shutting down VM
 09-19 09:23:09.525: W/dalvikvm(803): threadid=1: thread exiting with uncaught  exception (group=0x40a71930)
 09-19 09:23:09.535: E/AndroidRuntime(803): FATAL EXCEPTION: main
 09-19 09:23:09.535: E/AndroidRuntime(803): java.lang.NoClassDefFoundError:  com.google.android.gms.R$styleable
 09-19 09:23:09.535: E/AndroidRuntime(803):     at  com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at      android.app.Activity.onCreateView(Activity.java:4716)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at  android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.Activity.setContentView(Activity.java:1881)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at com.mypack.test.MainActivity.onCreate(MainActivity.java:17)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.Activity.performCreate(Activity.java:5104)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.os.Handler.dispatchMessage(Handler.java:99)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.os.Looper.loop(Looper.java:137)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at android.app.ActivityThread.main(ActivityThread.java:5041)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at java.lang.reflect.Method.invokeNative(Native Method)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at java.lang.reflect.Method.invoke(Method.java:511)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 09-19 09:23:09.535: E/AndroidRuntime(803):     at  dalvik.system.NativeStart.main(Native Method)

new logcat after editing the previous code..and loading playservice and v4.jar

  09-19 14:04:10.644: D/dalvikvm(1930): GC_CONCURRENT freed 1650K, 22% free  6514K/8284K, paused 76ms+19ms, total 279ms
  09-19 14:04:10.644: D/dalvikvm(1930): WAIT_FOR_CONCURRENT_GC blocked 51ms
  09-19 14:04:11.121: I/Choreographer(1930): Skipped 51 frames!  The application may be doing too much work on its main thread.
  09-19 14:04:13.281: I/Choreographer(1930): Skipped 170 frames!  The application may be doing too much work on its main thread.
  09-19 14:05:14.910: D/dalvikvm(1930): GC_CONCURRENT freed 85K, 7% free 7779K/8284K, paused 71ms+5ms, total 151ms
  09-19 14:05:14.910: D/dalvikvm(1930): WAIT_FOR_CONCURRENT_GC blocked 64ms
  09-19 14:05:15.040: E/MediaPlayer(1930): prepareAsync called in state 8
  09-19 14:05:15.040: W/System.err(1930): java.lang.IllegalStateException
  09-19 14:05:15.040: W/System.err(1930):   at android.media.MediaPlayer.prepare(Native Method)
  09-19 14:05:15.050: W/System.err(1930):   at androassist.mypack.splash.onCreate(splash.java:38)
  09-19 14:05:15.050: W/System.err(1930):   at android.app.Activity.performCreate(Activity.java:5104)
  09-19 14:05:15.060: W/System.err(1930):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  09-19 14:05:15.060: W/System.err(1930):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  09-19 14:05:15.060: W/System.err(1930):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  09-19 14:05:15.060: W/System.err(1930):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
  09-19 14:05:15.060: W/System.err(1930):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  09-19 14:05:15.060: W/System.err(1930):   at android.os.Handler.dispatchMessage(Handler.java:99)
  09-19 14:05:15.060: W/System.err(1930):   at android.os.Looper.loop(Looper.java:137)
  09-19 14:05:15.060: W/System.err(1930):   at android.app.ActivityThread.main(ActivityThread.java:5041)
  09-19 14:05:15.071: W/System.err(1930):   at java.lang.reflect.Method.invokeNative(Native Method)
  09-19 14:05:15.071: W/System.err(1930):   at java.lang.reflect.Method.invoke(Method.java:511)
  09-19 14:05:15.071: W/System.err(1930):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  09-19 14:05:15.080: W/System.err(1930):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  09-19 14:05:15.080: W/System.err(1930):   at dalvik.system.NativeStart.main(Native Method)
  09-19 14:05:20.561: I/Choreographer(1930): Skipped 39 frames!  The application may be doing too much work on its main thread.
  09-19 14:05:21.551: I/Choreographer(1930): Skipped 222 frames!  The application may be doing too much work on its main thread.
  09-19 14:05:22.491: I/Choreographer(1930): Skipped 225 frames!  The application may be doing too much work on its main thread.
  09-19 14:05:26.330: W/IInputConnectionWrapper(1930): showStatusIcon on inactive InputConnection
  09-19 14:05:28.770: D/Notice(1930): Entering log
   09-19 14:05:28.920: D/Key(1930): key matched
   09-19 14:05:32.620: I/Choreographer(1930): Skipped 783 frames!  The application may be doing too much work on its main thread.
   09-19 14:05:33.422: I/Choreographer(1930): Skipped 174 frames!  The application may be doing too much work on its main thread.
   09-19 14:05:35.290: I/Choreographer(1930): Skipped 55 frames!  The application may be doing too much work on its main thread.
   09-19 14:05:59.101: D/Notice(1930): Entering log
   09-19 14:05:59.831: I/Choreographer(1930): Skipped 48 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:00.600: I/MyApp(1930): Silent mode
   09-19 14:06:00.710: I/Choreographer(1930): Skipped 115 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:01.280: I/Choreographer(1930): Skipped 48 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:15.223: D/Notice(1930): Entering log
   09-19 14:06:15.900: I/Choreographer(1930): Skipped 54 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:16.712: I/MyApp(1930): Silent mode
   09-19 14:06:16.930: I/Choreographer(1930): Skipped 187 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:17.620: I/Choreographer(1930): Skipped 92 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:26.514: D/Notice(1930): Entering log
   09-19 14:06:27.103: I/Choreographer(1930): Skipped 50 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:28.241: D/GPS(1930): GPS Enabled
   09-19 14:06:28.265: D/loc message:(1930): fmygpslo(0.00.0)
   09-19 14:06:28.731: I/Choreographer(1930): Skipped 381 frames!  The application may be doing too much work on its main thread.
   09-19 14:06:34.700: I/Choreographer(1930): Skipped 177 frames!  The application may be doing too much work on its main thread.

Thank you for spending your valuable time.I got the solution...it was my mistake... As Reghunandan and Patrik said.It is problem with referencing google playstore services..my app references google playstore services from SDK directory,not from the loaded version of google playstore in my workspace.So the above errors are cleared..While running app the map is not loading..I checked it with the following code

     package myapp.mypack;

     import com.google.android.gms.common.ConnectionResult;
     import com.google.android.gms.common.GooglePlayServicesUtil;
     import com.google.android.gms.maps.GoogleMap;
     import com.google.android.gms.maps.SupportMapFragment;

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

     public class MapActivity extends FragmentActivity {

String msgtype,sendernum;
// Google Map
private GoogleMap googleMap;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(check()) 
    {
        setContentView(R.layout.app_main);
        Toast.makeText(this, "In Google play service.", Toast.LENGTH_LONG).show();
        initFun();
    }
    else
    {
        Toast.makeText(this, "Google play service not available.",      Toast.LENGTH_LONG).show();
    }
}

private Boolean check()
{
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if(status==ConnectionResult.SUCCESS)
    {
        return true;
    }

    return false;
}

public void initFun()
{
    SupportMapFragment sp = (SupportMapFragment)      getSupportFragmentManager().findFragmentById(R.id.map);
    googleMap = sp.getMap();
    googleMap.setMyLocationEnabled(true);
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


}

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

 }

Android manifest

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

    <permission
    android:name="myapp.mypack.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

    <uses-permission android:name="myapp.mypack.googlemapsv2.permission.MAPS_RECEIVE"/>

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

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission  android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
     <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="myapp.mypack.MapActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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

     </manifest>

But my map is loading...Instead it gives a while screen and displays "Google play service not available." as specified in the code..but i checked my project>properties>android. google playstore library is there with a green tick mark. So think it is the problem with my playstore version..*i run the app in my galaxy s3 mini.But it also shows the same white screen..my playstore build version in phone is:4.3.11.The only solution i have is to update playstore and rerun my app.The logcat also shows my mobile phone's playsore version need to be updated..Guys please have a look at my queries..Is it the problem with my phone's playstore version..*

logcat

   09-20 01:19:51.980: E/Trace(26515): error opening trace file: No such file or directory (2)
   09-20 01:19:52.020: W/GooglePlayServicesUtil(26515): Google Play services out of date.  Requires 3264100 but found 3225134
   09-20 01:19:52.190: D/libEGL(26515): loaded /system/lib/egl/libEGL_mali.so
   09-20 01:19:52.210: D/libEGL(26515): loaded /system/lib/egl/libGLESv1_CM_mali.so
   09-20 01:19:52.210: D/libEGL(26515): loaded /system/lib/egl/libGLESv2_mali.so
   09-20 01:19:52.240: D/OpenGLRenderer(26515): Enabling debug mode 0
 **Please help me to find a solution?**
Anish Ubais
  • 33
  • 2
  • 12
  • have you referenced google play services library project in your android map project?? – Raghunandan Sep 19 '13 at 10:00
  • Please add google-play-services-lib as a library project with your project. – Pratik Dasa Sep 19 '13 at 10:03
  • thanks for the response..yes i have referenced google play service...but it comes under android private libraries..i saw a solution in stackoverflow saying that google play service should be import as .jar outside of libaries folder..i also created a folder lib and put google_play_services.jar and android-support-V4.jar.. – Anish Ubais Sep 19 '13 at 10:14
  • @AnishUbais you should not put the jar in libs folder. you need to reference the same in your android map project – Raghunandan Sep 19 '13 at 10:20
  • @Reghunandan..please tell me how to reference jars...thank you – Anish Ubais Sep 19 '13 at 10:23
  • Guys please have a look at this link...http://stackoverflow.com/questions/14230808/google-map-android-api-v2-crashed?rq=1 – Anish Ubais Sep 19 '13 at 10:24
  • @AnishUbais do check this http://stackoverflow.com/questions/17611017/importing-google-play-service-library-showing-a-red-x-next-to-this-reference-and/17611095#17611095. You need to reference the library project not add it as a jar – Raghunandan Sep 19 '13 at 10:32
  • Thank you @Raghunandan....i will check your answer...please look at my new logcat...please tell me what it is..my app get crashed but no error in logcat? – Anish Ubais Sep 19 '13 at 14:35

1 Answers1

0
  private void initilizeMap() {
    if (googleMap == null) {
        MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        googleMap = mapFragment.getMap();

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

run it on device..

swati srivastav
  • 635
  • 4
  • 15