uma uma - 3 months ago 32
Android Question

android.view.InflateException: Binary XML file line #8: Error inflating class fragment

I am trying to make Google Map activity to find travel distance.But Unfortunately layout.xml file throw , below error and stop app. I used Min SDK version - 16.
I tried lot of thing to do but still error is remain.I run my app in real device.Expect some expert help.

2218/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{scpp.globaleye.com.scppclient/scpp.globaleye.com.scppclient.ui.MapsActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.access$600(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
at android.app.Activity.setContentView(Activity.java:1867)
at scpp.globaleye.com.scppclient.ui.MapsActivity.onCreate(MapsActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211) 
at android.app.ActivityThread.access$600(ActivityThread.java:149) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:153) 
at android.app.ActivityThread.main(ActivityThread.java:4987) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at com.google.maps.api.android.lib6.gmm6.m.ad.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.c.h.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.c.y.a(Unknown Source)
at com.google.maps.api.android.lib6.e.bd.a(Unknown Source)
at com.google.maps.api.android.lib6.e.ev.a(Unknown Source)
at com.google.maps.api.android.lib6.e.z.a(Unknown Source)
at com.google.maps.api.android.lib6.e.y.a(Unknown Source)
at com.google.android.gms.maps.internal.u.onTransact(SourceFile:107)
at android.os.Binder.transact(Binder.java:326)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment$zza.onCreateView(Unknown Source)
at com.google.android.gms.dynamic.zza$4.zzb(Unknown Source)
at com.google.android.gms.dynamic.zza.zza(Unknown Source)
at com.google.android.gms.dynamic.zza.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
at android.app.Activity.onCreateView(Activity.java:4676)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258) 
at android.app.Activity.setContentView(Activity.java:1867) 
at scpp.globaleye.com.scppclient.ui.MapsActivity.onCreate(MapsActivity.java:44) 
at android.app.Activity.performCreate(Activity.java:5020) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211) 
at android.app.ActivityThread.access$600(ActivityThread.java:149) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:153) 
at android.app.ActivityThread.main(ActivityThread.java:4987) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
at dalvik.system.NativeStart.main(Native Method) 


My MapsActivity class. I comment other line and find
setContentView(R.layout.activity_maps);
pass this error and stop app.

public class MapsActivity extends FragmentActivity implements LocationListener {

GoogleMap googleMap;
double start_lat,stop_lat,start_lng,stop_lng,lat,lng;
Criteria criteria;
LocationManager locationManager;
String provider;
Location location;
Button start_btn,stop_btn;
TextView tv;
float[] distance = new float[1];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_maps);
//MapFragment fm = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

//System.out.println("on createteeeeeeeeeeeeeee");

//SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

/*googleMap = fm.getMap();
getLocation();

//////button
start_btn = (Button) findViewById(R.id.button1);
stop_btn = (Button) findViewById(R.id.button2);
stop_btn.setEnabled(false);

start_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//System.out.println("startttttttttttttttttttttttttbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
if(tv != null){
tv.setText("");
}

stop_btn.setEnabled(true);
start_btn.setEnabled(false);
getStarrtLoc(location);

}
});

stop_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//System.out.println("stoppppppppppppppppppppppppppbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
start_btn.setEnabled(true);
stop_btn.setEnabled(false);
getStoptLoc(location);
}
});*/

}
@Override
public void onLocationChanged(Location loc) {

location = loc;
//System.out.println("changggggggggggggggggggggggggg"+loc.getLatitude()+loc.getLongitude());
}

@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
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

public Location getLocation(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//return;
Toast.makeText(this,"NO PERMISSION", Toast.LENGTH_LONG).show();
}
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);

// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// Creating a criteria object to retrieve provider
criteria = new Criteria();

// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);

// Getting Current Location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//return;
Toast.makeText(this,"NO PERMISSION", Toast.LENGTH_LONG).show();
}
location = locationManager.getLastKnownLocation(provider);

if(location!=null){
Toast.makeText(this,"GPS FIXED", Toast.LENGTH_LONG).show();
//onLocationChanged(location);
//return location;
lat=location.getLatitude();
lng=location.getLongitude();
}
locationManager.requestLocationUpdates(provider,0,0, this);
//System.out.println("llllllllllllllllllllllllllllllllllllll"+lat+lng);
return location;
}

public void getStarrtLoc(Location start_loc){
start_lat = start_loc.getLatitude();
start_lng = start_loc.getLongitude();
//System.out.println("startttttttttttttttttttttttttttttttttt"+start_lat+"ffffffffff"+start_lng);

}

public void getStoptLoc(Location stop_loc){

stop_lat = stop_loc.getLatitude();
stop_lng = stop_loc.getLongitude();
//System.out.println("stppppppppppppppppppppppppppppppppppppp"+stop_lat+"ffffffffff"+stop_lng);
calcDistance();
tv = (TextView) findViewById(R.id.textView);
tv.setText("Distance :"+distance[0]);
//System.out.println("dddddddddd"+distance[0]);
}

public void calcDistance(){
Location.distanceBetween(start_lat,start_lng,stop_lat,stop_lng,distance);
}

@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(MapsActivity.this, UserSelect.class);
MapsActivity.this.startActivity(intent);
MapsActivity.this.finish();
}
}


My Activity_Map.xml file.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"
tools:context=".ui.MapsActivity">

<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/layout1"
tools:layout="@layout/activity_maps" />

<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/layout1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true">

<Button
android:layout_width="145dp"
android:layout_height="70dp"
android:text="Start Ride"
android:id="@+id/button1"
android:layout_alignParentLeft="true"/>

<Button
android:layout_width="145dp"
android:layout_height="70dp"
android:text="Stop Ride"
android:id="@+id/button2"
android:layout_alignParentRight="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text=""
android:id="@+id/textView"
android:layout_toLeftOf="@+id/button2"
android:layout_toRightOf="@+id/button1"
android:layout_toStartOf="@id/button2"
android:layout_toEndOf="@+id/button1" />

</RelativeLayout>


</RelativeLayout>


my androidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="scpp.globaleye.com.scppclient">


<uses-sdk
android:minSdkVersion="16"
/>

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />

<!-- To boot complete broadcast receiver -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- External storage for caching -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!--
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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_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="AIzaSyBLN7wET4T0n6gu6Jr3AwE6jOQpqICkMBE" />

<service
android:name=".services.RemoteSenzService"
android:enabled="true"
android:exported="true"></service>

<activity android:name=".ui.Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<activity android:name=".ui.Home" />
<activity android:name=".ui.Services_View" />
<activity android:name=".ui.Update_Profile" />
<activity android:name=".ui.UserSelect" />
<activity android:name=".ui.Wallet" />
<activity android:name=".ui.Registration" />
<activity
android:name=".ui.MapsActivity"
android:label="@string/title_activity_maps">
</activity>


</application>

</manifest>

Answer

I think that is because you are using a FragmentActivity, so you should use the SupportMapFragment and not the MapFragment (Because the support API Activity works with the Fragment class of the support library and not with the one of the base framework). Try changing this line

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

to this

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

As a side note, it's important to note that you should also use the AppCompatActivity instead of the FragmentActivity.

Comments