Reputation: 825
I just begin to learn Android. Full of my day I try to use Google Map on Android but everytime I run it on Emulator, it gives the notification of "Unfortunately, has stopped." Here's my code: 1. Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapmapmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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.example.mapmapmap.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCSOXImKxtdCbhph6WAW6QammSm7rDa9lA" />
</application>
</manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity.java:
package com.example.mapmapmap;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
LogCat:
04-26 09:32:54.001: E/Trace(2210): error opening trace file: No such file or directory (2)
04-26 09:32:55.111: D/AndroidRuntime(2210): Shutting down VM
04-26 09:32:55.111: W/dalvikvm(2210): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-26 09:32:55.141: E/AndroidRuntime(2210): FATAL EXCEPTION: main
04-26 09:32:55.141: E/AndroidRuntime(2210): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapmapmap/com.example.mapmapmap.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.os.Looper.loop(Looper.java:137)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-26 09:32:55.141: E/AndroidRuntime(2210): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 09:32:55.141: E/AndroidRuntime(2210): at java.lang.reflect.Method.invoke(Method.java:511)
04-26 09:32:55.141: E/AndroidRuntime(2210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-26 09:32:55.141: E/AndroidRuntime(2210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-26 09:32:55.141: E/AndroidRuntime(2210): at dalvik.system.NativeStart.main(Native Method)
04-26 09:32:55.141: E/AndroidRuntime(2210): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-26 09:32:55.141: E/AndroidRuntime(2210): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.Activity.setContentView(Activity.java:1881)
04-26 09:32:55.141: E/AndroidRuntime(2210): at com.example.mapmapmap.MainActivity.onCreate(MainActivity.java:11)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.Activity.performCreate(Activity.java:5104)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-26 09:32:55.141: E/AndroidRuntime(2210): ... 11 more
04-26 09:32:55.141: E/AndroidRuntime(2210): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.Fragment.instantiate(Fragment.java:592)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.Fragment.instantiate(Fragment.java:560)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.Activity.onCreateView(Activity.java:4709)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-26 09:32:55.141: E/AndroidRuntime(2210): ... 20 more
04-26 09:32:55.141: E/AndroidRuntime(2210): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.example.mapmapmap-1.apk
04-26 09:32:55.141: E/AndroidRuntime(2210): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
04-26 09:32:55.141: E/AndroidRuntime(2210): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-26 09:32:55.141: E/AndroidRuntime(2210): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-26 09:32:55.141: E/AndroidRuntime(2210): at android.app.Fragment.instantiate(Fragment.java:582)
04-26 09:32:55.141: E/AndroidRuntime(2210): ... 23 more
I greatly appreciate any attempts to help !
Upvotes: 0
Views: 1888
Reputation: 1154
Try This code will work succesfully
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
package com.towntour.android;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class PlacesMapActivity extends android.support.v4.app.FragmentActivity
implements OnClickListener, LocationListener {
/**
* Note that this may be null if the Google Play services APK is not
* available.
*/
ImageButton btn_home;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment fragment = (SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mMap = fragment.getMap();
mMap.setMyLocationEnabled(true);
// mMap = ((SupportMapFragment) getSupportFragmentManager()
// .findFragmentById(R.id.map)).getMap();
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title("First Location");
markerOptions.snippet("This Is Test Location");
LatLng latlng = new LatLng(23.0333, 72.6167);
markerOptions.position(latlng);
// markerOptions.title("Ahmedabad Cordinat Found here");
// Marker m = mMap.addMarker(markerOptions);
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View myContentView = getLayoutInflater().inflate(
R.layout.custommarker, null);
TextView tvTitle = ((TextView) myContentView
.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet = ((TextView) myContentView
.findViewById(R.id.snippet));
tvSnippet.setText(marker.getSnippet());
return myContentView;
}
});
mMap.addMarker(new MarkerOptions()
.position(latlng)
.title("This is Sabarmati Ashram")
.snippet("Ahmedabad")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),
DetailsOfPlacesActivity.class);
startActivity(intent);
}
});
btn_home = (ImageButton) findViewById(R.id.activity_map_ibtn_home);
btn_home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
super.onBackPressed();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
finish();
super.onPause();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.towntour.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.towntour.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="com.towntour.android.maps.googlev2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.towntour.android.maps.googlev2.permission.MAPS_RECEIVE"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDp6PHK3NYNwS74cKGmX-xc4LuH_uM1mIo" />
<activity
android:name="com.towntour.android.PlaceActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 2
Reputation: 1006869
Your error suggests that you manually modified the build path of your project to reference a JAR. That would represent two mistakes:
Never manually modify the build path of a standard Android app project
The Play Services SDK is an Android library project, not merely a JAR
The instructions for attaching the Play Services SDK to your project can be found in the Play Services SDK documentation.
Upvotes: 1