This question already has answers here:
Error Inflating class fragment in activity maps xml
(3 answers)
Closed 6 years ago.
I just wanted to run a simple map fragment but I keep getting InflateException. I also asked a similar question but I made a new Google Maps project for simplicity.
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: Process: com.example.ali.myapplication, PID: 1785
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ali.myapplication/com.example.ali.myapplication.MapsActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
MapsActivity.java:
package com.example.ali.myapplication;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
activity_maps.xml:
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#619ec9"
xmlns:android="http://schemas.android.com/apk/res/android">
>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map" tools:context=".MapsActivity"
class="com.google.android.gms.maps.SupportMapFragment"
tools:layout="#layout/activity_maps"
android:layout_width="362dp"
android:layout_height="323dp"
/>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ali.myapplication" >
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Fragment should be added to a frame layour, it cant be directly used.
Update your code and let me know if works
Related
My application worked yesterday, I cant figure out why it stopped working. The only thing I tried doing was to create a signed apk. to do this I had to create a new Google maps key and place it into the manifest file. I done this following the Google developers tutorial. But now when I try to run the app it start up, I click a button and it force closes.
This is the error log:
03-20 23:31:33.636 10036-10036/project.sharethefare E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: project.sharethefare, PID: 10036
java.lang.RuntimeException: Unable to start activity ComponentInfo{project.sharethefare/project.sharethefare.CurrentLocation}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
at android.app.Activity.setContentView(Activity.java:2241)
at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml
at com.google.maps.api.android.lib6.gmm6.c.g.a(Unknown Source)
at com.google.maps.api.android.lib6.c.i.a(Unknown Source)
at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
at com.google.maps.api.android.lib6.c.ab.a(Unknown Source)
at com.google.maps.api.android.lib6.c.aa.a(Unknown Source)
at com.google.android.gms.maps.internal.x.onTransact(SourceFile:107)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
at com.google.android.gms.dynamic.a$4.b(Unknown Source)
at com.google.android.gms.dynamic.a.a(Unknown Source)
at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
at android.app.Activity.setContentView(Activity.java:2241)
at project.sharethefare.CurrentLocation.onCreate(CurrentLocation.java:24)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
This is the CurrentLocation class:
package project.sharethefare;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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.LatLng;
import android.location.Location;
import android.view.View;
public class CurrentLocation extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
//________________________________________________________________________________________
#Override
protected void onCreate(Bundle savedInstanceState) { //auto generated
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_location);
setUpMapIfNeeded(); //part of google maps api
mMap.setMyLocationEnabled(true); //creates a new HomeScreen
}
#Override
protected void onResume() { //Auto Generated
super.onResume();
setUpMapIfNeeded(); //set up map if not already created
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {#link #setUpMap()} once when {#link #mMap} is not null.
* <p/>
* If it isn't installed {#link SupportMapFragment} (and
* {#link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {#link #onCreate(Bundle)} may not be called again so we should call this
* method in {#link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.curLocMap))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {#link #mMap} is not null.
*/
//call the method to continuously check current location
private void setUpMap() {
mMap.setOnMyLocationChangeListener(myLocationChangeListener);
}
//called above. Used to constantly update the users position on the map
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
//create a new latitude and longitude point
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
//make global variables on home screen == to current location
HomeScreen.curLat = location.getLatitude();
HomeScreen.curLong = location.getLongitude();
HomeScreen.curLocSet = true;
//animate the camera to zoom in on position when found
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
};
public void backToHome(View view){ // called when button clicked. returns to homeScreen activity
Intent intent = new Intent(CurrentLocation.this,HomeScreen.class);
startActivity(intent);
}
}
This is the corresponding xml file:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#000">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginTop="60dp"
android:layout_marginBottom="50dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/curLocMap"
tools:context="project.sharethefare.CurrentLocation"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="70dp"
android:text="#string/set_Cur_Loc"
android:textSize="20sp"
android:onClick="backToHome"
android:background="#drawable/button_design"
android:textStyle="bold"/>
</LinearLayout>
And this is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="project.sharethefare" >
<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" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!--
The ACCESS_COARSE/FINE_LOCATION 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" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HomeScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="" />
<activity
android:name=".CurrentLocation"
android:label="#string/title_activity_current_location" >
</activity>
<activity
android:name=".Destination"
android:label="#string/title_activity_maps" >
</activity>
<activity
android:name=".Share"
android:label="#string/title_activity_share" >
</activity>
<activity
android:name=".SplashScreen"
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.geo.API_KEY"
android:value=""/>
</application>
</manifest>
Caused by: java.lang.RuntimeException: The API key can only be specified once. It is recommended that you use the meta-data tag with the name: com.google.android.maps.v2.API_KEY in the <application> element of AndroidManifest.xml
Based upon the error message, delete this from your manifest:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>
If you are using both Maps & Places Api in your application then you only need to specify geo api key
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCgOCmQbNnBUd_6HeB5DmTfHXPaxmmJJtk"/>
Both apis will work properly.You can not use both meta data tags for google apis.
In my case I have Google Map & Place API (PlaceLikelihoodBuffer).
Manifest file does not support two meta data for Google API key.
I have commented Google map API key for Map. Looks like below:
<!-- <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBqXxRIxL4nK7BarmN6qQB48kkAbdp1Kbk" />\ -->
<meta-data
android:name="com.crashlytics.ApiKey"
android:value="5b03ad6e904c61e5d9bfe6e499e749671d9eaea4" />
Fortunately Google Map & Place API (PlaceLikelihoodBuffer) both worked for me.
Strange but true.
Hope this will help you.
I create an app view maps using google maps api v2. This is my code:
My Activity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
}
My main_activity:
<?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"
android:name="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
Android manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyB96b4lWziSteDtMTqrDDurcTvOIwzKCRE" />
And when I running, this is logcat:
07-21 17:53:33.734: E/AndroidRuntime(3558): FATAL EXCEPTION: main
07-21 17:53:33.734: E/AndroidRuntime(3558): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demogooglemap/com.example.demogooglemap.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.access$700(ActivityThread.java:151)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.os.Looper.loop(Looper.java:137)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-21 17:53:33.734: E/AndroidRuntime(3558): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 17:53:33.734: E/AndroidRuntime(3558): at java.lang.reflect.Method.invoke(Method.java:511)
07-21 17:53:33.734: E/AndroidRuntime(3558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-21 17:53:33.734: E/AndroidRuntime(3558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-21 17:53:33.734: E/AndroidRuntime(3558): at dalvik.system.NativeStart.main(Native Method)
07-21 17:53:33.734: E/AndroidRuntime(3558): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
Plz help!
Your min sdk is 8
android:minSdkVersion="8"
Below api level 11 you need to use SupportMapFragment and extend FragmentActivity.
Change this
android:name="com.google.android.gms.maps.MapFragment"
to
android:name="com.google.android.gms.maps.SupportMapFragment"
Change this
public class MainActivity extends Activity
to
public class MainActivity extends FragmentActivity
Missing few permissions in manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 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"/>
Follow
https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2
Just adding on to what #Raghunandan has said, you also need to make sure the device knows what version of google play services you are using. implement this into your manafest too (inside application).
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Also make sure you have imported google-play-services_lib into your workspace and declared that your project is using the services (in the android properties of said project).
I am a beginning in trying to create applications for android using Eclipse (also why i didn't do pictures). I started to learn to program by following Site (link). I tried this 3 times and each times it creates 2 projects. First one always called 'appcompat_v7' and the second one what ever i called it. In this instance I called it 'FirstApp'. In the project FirstApp, I could not find /res/layout containing anything so i created it, calling it fragment_main.xml. Containing:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</LinearLayout>
I edited my 'androidmanifest.xml' to say:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And my strings.xml says:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">FirstApp</string>
<string name="edit_message">Enter a message</string>
</resources>
When running on my Samsung Galaxy S4 or on a AVD manager:
(settings are platform: 4.4W , API Lvl: 20 , and CPU/ABI: Android Wear
ARM (armeabi-v7a) , Device: Nexus 5)
Both give me the same error. "Unfortunately, (app name) has stopped."
And my LogCat gives me this log:
~Not late-enabling CheckJNI (already on)
~Shutting down VM
~threadid=1: thread exiting with uncaught exception (group=0xb2aa9d70)
~FATAL EXCEPTION: main
~Process: com.example.firstapp, PID: 745
~java.lang.RuntimeException: Unable to instantiate activity ComponentI
nfo{com.example.firstapp/com.example.firstapp.MainActivity}: java.lan
g.ClassNotFoundException: Didn't find class "com.example.firstapp.Mai
nActivity" on path: DexPathList[[zip file "/data/app/com.example.firs
tapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.firs
tapp-2, /system/lib]]
~at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2123)
~at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
~at android.app.ActivityThread.access$800(ActivityThread.java:138)
~at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
~at android.os.Handler.dispatchMessage(Handler.java:102)
~at android.os.Looper.loop(Looper.java:136)
~at android.app.ActivityThread.main(ActivityThread.java:5026)
~at java.lang.reflect.Method.invokeNative(Native Method)
~at java.lang.reflect.Method.invoke(Method.java:515)
~at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
~at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
~at dalvik.system.NativeStart.main(Native Method)
~Caused by: java.lang.ClassNotFoundException: Didn't find class "com.e
xample.firstapp.MainActivity" on path: DexPathList[[zip file "/data/a
pp/com.example.firstapp-2.apk"],nativeLibraryDirectories=[/data/app-l
ib/com.example.firstapp-2, /system/lib]]
~at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
~at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
~at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
~at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
~at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
~ ... 11 more
~Sending signal. PID: 745 SIG: 9
I have not edited anything else, but those xml files (strings.xml, fragment_main.xml, and AndroidManifest.xml)
Here is a pic of my packages:
Please help to remove this error. Thanks!
First of all, the reason that your app is creating two projects is for compatibility with devices running below API 14. I would recommend (and this is just a personal preference) that, as a beginner, when you create your projects you should set the minimum API level to 14.
When setting up a new project, check the check box that says 'Create activity', right under 'Create custom launcher icon':
Among the automatically created files, you should find a MainActivity.java file (in src > com.example.YourAppName) and an activity_main.xml layout file (in res > layout). If they are not created, this points to issues with your Eclipse/ADT/SDK setup.
So my advice would be to create a new project (above API 14) and run it without editing/creating any files. If it works, then you're good to go.
I have the launch activity called MainActivity that will eventually start another called SearchResultsActivity.
They both extend ActionBarActivity, and the second is yet to be written, for now it is just a stub with:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.search_results);
}
The layout XML is as follows, just a ListView in a white background:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_products"
android:layout_gravity="center_horizontal" />
</LinearLayout>
The app is crashing on setContentView() with the following logcat:
17348-17348/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SearchResultsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3714)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
at com.example.myapp.SearchResultsActivity.onCreate(SearchResultsActivity.java:42) <-- this is setContentView()
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1630)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3714)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Here's the manifest excerpt:
<activity
android:name="com.example.myapp.MainActivity"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Styled">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.SearchResultsActivity"
android:label="#string/app_name"
android:theme="#style/Theme.Styled"
android:configChanges="orientation|keyboardHidden">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapp.MainActivity" />
</activity>
I thought it could be some issue with the layout, but then I decide to replace R.layout.search_results with R.layout.home, which is the same XML used in MainActivity, therefore should work. But it crashes in the very same line.
Any ideas on what could be causing this?
As others have pointed out, your error is likely in your onCreate code for your second activity. If you read the documentation here you'll see they start their activity off with onCreate and an immediate call back to super.onCreate(...)
Your code should be as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //added this line
setContentView(R.layout.search_results);
}
If you chose to have your IDE automatically generate an Activity stub, it will always place the call to super.onCreate in your activity. It initializes the activity, see this question for more of an explanation.
If this doesn't fix your problem and you have more code in your second activity, it might be useful to post code for the entire activity.
I'm trying to display google map on my android app when user click on a button.
when I click the button the app stops.No error in the code.
I believe I've done everything right
1-I got the API KEY
2-I added the Google_Play_Services_lib.jar to the dependencies folder
3-added extra support tool
4-included the permissions,my key to the manifest
5-added fragment to my xml layout fie for the map
please help I feel like I've read and watched every tutorial there is...nothing worked!
My Log:
03-10 20:27:10.057: I/Process(17287): Sending signal. PID: 17287 SIG: 9
03-10 20:27:15.072: D/libEGL(17688): loaded /vendor/lib/egl/libEGL_adreno.so
03-10 20:27:15.072: D/libEGL(17688): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
03-10 20:27:15.082: D/libEGL(17688): loaded /vendor/lib/egl/libGLESv2_adreno.so
03-10 20:27:15.082: I/Adreno-EGL(17688): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
03-10 20:27:15.082: I/Adreno-EGL(17688): OpenGL ES Shader Compiler Version: 17.01.10.SPL
03-10 20:27:15.082: I/Adreno-EGL(17688): Build Date: 09/26/13 Thu
03-10 20:27:15.082: I/Adreno-EGL(17688): Local Branch:
03-10 20:27:15.082: I/Adreno-EGL(17688): Remote Branch:
03-10 20:27:15.082: I/Adreno-EGL(17688): Local Patches:
03-10 20:27:15.082: I/Adreno-EGL(17688): Reconstruct Branch:
03-10 20:27:15.122: D/OpenGLRenderer(17688): Enabling debug mode 0
03-10 20:27:16.683: D/AndroidRuntime(17688): Shutting down VM
03-10 20:27:16.683: W/dalvikvm(17688): threadid=1: thread exiting with uncaught exception (group=0x41931898)
03-10 20:27:16.693: E/AndroidRuntime(17688): FATAL EXCEPTION: main
03-10 20:27:16.693: E/AndroidRuntime(17688): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newandroid/com.example.newandroid.Mymap}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-10 20:27:16.693: E/AndroidRuntime(17688): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
03-10 20:27:16.693: E/AndroidRuntime(17688): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
03-10 20:27:16.693: E/AndroidRuntime(17688): at android.app.ActivityThread.access$700(ActivityThread.java:159)
My code for Mymap activity
p
ackage com.example.newandroid;
import android.os.Bundle;
//import android.app.Activity;
import android.support.v4.app.FragmentActivity;
public class Mymap extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mymap);
}
}
My layout
<?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:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.newandroid"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.newandroid.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.newandroid.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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="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>
<activity
android:name="com.example.newandroid.Mymap"
android:label="#string/title_activity_mymap"
android:parentActivityName="com.example.newandroid.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.newandroid.MainActivity" />
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAq44a-wWB4W6muJJqZ1DMH-livYscbiyk"/>
</application>
</manifest>
this is the MainActivity where the button is ,when clicked it should start Mymap activity.this is Mainactivity code
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
}
public void view_map(View view) {
Intent intent = new Intent(this, Mymap.class);
startActivity(intent);
// Do something in response to button
}
}
have you tried to look at this SO post?
seems like changing the xml to match your class name fixes most issues.
Google Maps V2 - Error inflating class Fragment
If you are targeting api level 8 and above then you can't use a simple Activity, you need to extend FragmentActivity.
I'm guessing this is the reason you have the inflating problem.
And you are missing this meta-data section in you manifest file:
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
place it next to your's api key meta-data section.
I did use Google Maps v2 API for android in my recent app development and I cannot even start to tell you how long it took to just have the map show up on the screen. I eventually got it working and I have the project on Github which you can look through and make sure to read through all the important files like Manifest and res/ folder;
Google Maps API (Android) v2 Fragment Issues
I hope it helps!