-1

I am working on a map app. It's working for me on SDK 16 but the problem was I need to expand it on sdk 8 and my app crashes. This is my whole code for sdk 16 here

i added support-library-v4 as externar JAR to project. i also added google-play-service-lib as library. but nothing works

I changed Activity to FragmentActivity and also used

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

instead of

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

I had many problems but finally solved it on sdk 16. I need to use that in sdk 8 but app crashes and doesn't work.

plz help me on this.

this is my manifest:

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


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



    <uses-permission android:name="com.ariagostar.maptest.permission.MAPS_RECEIVE" />
    <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/mahdi_theme"
        android:name=".G"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" 
            >

        </activity>

        <activity android:name=".SplashActivity">



        </activity>
        <activity android:name=".LocationsActivity"
            android:screenOrientation="portrait"
            >


        </activity>
        <activity android:name=".AIDSActivity"
            android:screenOrientation="portrait"
           ></activity>
        <activity android:name=".AboutActivity"></activity>
        <activity android:name=".CureActivity"
            android:screenOrientation="portrait"
           ></activity>
        <activity android:name=".EllatActivity"
            android:screenOrientation="portrait"
            ></activity>
        <activity android:name=".InfectedActivity"
            android:screenOrientation="portrait"
            ></activity>
        <activity android:name=".QuestionActivity"
            android:screenOrientation="portrait"
            >




        </activity>

        <activity android:name=".SeirActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".PreventionActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".TashdidActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".AboutUsActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".OtherProductActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".MapActivity"
           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>



      <uses-library android:name="com.google.android.maps" />

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

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

    </application>

and this is my map activity:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;


public class MapActivity extends FragmentActivity {



         private GoogleMap googleMap;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_form);
            try {
                Log.i("LOG", "map test 1");
                // Loading map
                initilizeMap();
                Log.i("LOG", "map test 2");

            } catch (Exception e) {
                 Log.e("LOG", "I got an error", e);
                e.printStackTrace();
            }

            Button b1 = (Button) findViewById(R.id.button1);

            b1.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    double latitude =20.002 ;
                    double longitude = 3.399;

                    // create marker
                    //MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");

                    // adding marker
                    //googleMap.addMarker(marker);

                    CameraPosition cameraPosition = new CameraPosition.Builder().target(
                            new LatLng(37.385044, 78.486671)).zoom(12).build();

                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                }
            });
        }

        @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();
                //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();
                }
            }
        }

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


}

and this is my map_form

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

logcat:

12-13 11:09:51.957: E/LOG(4706): I got an error
12-13 11:09:51.957: E/LOG(4706): java.lang.NullPointerException
12-13 11:09:51.957: E/LOG(4706):    at com.ariagostar.hiv.MapActivity.initilizeMap(MapActivity.java:76)
12-13 11:09:51.957: E/LOG(4706):    at com.ariagostar.hiv.MapActivity.onCreate(MapActivity.java:32)
12-13 11:09:51.957: E/LOG(4706):    at android.app.Activity.performCreate(Activity.java:5372)
12-13 11:09:51.957: E/LOG(4706):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
12-13 11:09:51.957: E/LOG(4706):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 11:09:51.957: E/LOG(4706):    at android.os.Looper.loop(Looper.java:137)
12-13 11:09:51.957: E/LOG(4706):    at android.app.ActivityThread.main(ActivityThread.java:5455)
12-13 11:09:51.957: E/LOG(4706):    at java.lang.reflect.Method.invokeNative(Native Method)
12-13 11:09:51.957: E/LOG(4706):    at java.lang.reflect.Method.invoke(Method.java:525)
12-13 11:09:51.957: E/LOG(4706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-13 11:09:51.957: E/LOG(4706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-13 11:09:51.957: E/LOG(4706):    at dalvik.system.NativeStart.main(Native Method)
12-13 11:09:51.957: E/AndroidRuntime(4706): FATAL EXCEPTION: main
12-13 11:09:51.957: E/AndroidRuntime(4706): java.lang.RuntimeException: Unable to resume activity {com.ariagostar.hiv/com.ariagostar.hiv.MapActivity}: java.lang.NullPointerException
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2929)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2958)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2364)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.access$700(ActivityThread.java:165)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.os.Looper.loop(Looper.java:137)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.main(ActivityThread.java:5455)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at java.lang.reflect.Method.invoke(Method.java:525)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at dalvik.system.NativeStart.main(Native Method)
12-13 11:09:51.957: E/AndroidRuntime(4706): Caused by: java.lang.NullPointerException
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.ariagostar.hiv.MapActivity.initilizeMap(MapActivity.java:76)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at com.ariagostar.hiv.MapActivity.onResume(MapActivity.java:90)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.Activity.performResume(Activity.java:5450)
12-13 11:09:51.957: E/AndroidRuntime(4706):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919)
12-13 11:09:51.957: E/AndroidRuntime(4706):     ... 12 more
Community
  • 1
  • 1
Mahdi
  • 6,139
  • 9
  • 57
  • 109
  • Where is your error ? – GrIsHu Dec 10 '13 at 06:55
  • @GrIsHu: in emulator it says that: INSTALL_FAILED_MISSING_SHARED_LIBRARY and cancels the istalation. in my real device "unfortunately ... has stoped" – Mahdi Dec 10 '13 at 06:57
  • @Kenji Google Maps API V2 does not work on emulator, at all. – rahulritesh Dec 10 '13 at 07:03
  • so what i should do? this code worked on SDK 16 and it's still installed on my dev. but have crashes on SDK 8 :( – Mahdi Dec 10 '13 at 07:08
  • see the following link for help you in Google Map [Google Map][1] [1]: http://stackoverflow.com/questions/20464136/android-google-maps-api-v2-requires-permission-that-it-already-has/20464609#20464609 – Mohammed Saleem Dec 10 '13 at 07:08
  • as you can see in my manifest i dont think the problem is from permissions. but any way i add all that permissions you told me. but still crashesh – Mahdi Dec 10 '13 at 07:11
  • No crash log added how one will identify your problem ? – GrIsHu Dec 10 '13 at 07:28
  • @GrIsHu: you right. i added the log. – Mahdi Dec 10 '13 at 07:43
  • In your `onResume()` method write the `super.onResume();` and then write ` initilizeMap();`. – GrIsHu Dec 10 '13 at 07:55

4 Answers4

1

The MapFragment or SupportMapFragment is supported only after API Level 12. It is recommended to use the SupportMapFragment instead.

The Fragment class is not available until API Level 11. and you are targeting API Level 8 which is not supporting Fragment. and thats why i think you are getting the error like below:

Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

You need to import Google Play Services in project.

Check whether you have already downloaded Google Play Services in Android SDK Manager

For more information Check this example

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • i added support-library-v4 as externar JAR to project. i also added google-play-service-lib as library. but nothing works – Mahdi Dec 10 '13 at 07:06
0

As you error indicates INSTALL_FAILED_MISSING_SHARED_LIBRARY and cancels the istalation. in my real device "unfortunately ... has stoped" and as you are using the SupportMapFragment and its available in supportv4 library so are supposed to add the support-library-v4 in your project to support the map fragment below the API12.

Change your onResume() as below:

   @Override
    protected void onResume() {
         super.onResume();
         initilizeMap();
    }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • i added support-library-v4 as externar JAR to project. i also added google-play-service-lib as library. but nothing works – Mahdi Dec 10 '13 at 07:05
  • thank you. but i finally solved the problem. it was because of map_form – Mahdi Dec 10 '13 at 07:59
0

i finally solved the problem. it was just because of using

android:name="com.google.android.gms.maps.MapFragment"

and should use this one:

android:name="com.google.android.gms.maps.SupportMapFragment"

and the apps works correctly

Mahdi
  • 6,139
  • 9
  • 57
  • 109