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
i finally solved the problem. it was just because of using
and should use this one:
and the apps works correctly
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:
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 theSupportMapFragment
and its available in supportv4 library so are supposed to add thesupport-library-v4
in your project to support the map fragment below the API12.Change your
onResume()
as below: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