Md. Imran Choudhury Md. Imran Choudhury - 3 months ago 30
Android Question

Error when try to add map inside fragement TAB

I am try to add map inside fragment tab. My google map normally works. But when i use map inside tab fragment child then get exception:

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


I want to add map in TAB2

enter image description here


MainActivity.java


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("");
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText("Chili's");
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_restaurant_white).setText("Menu"));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_place_white).setText("Map"));;
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_thumb_up_white).setText("Facebook"));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_info_white).setText("About"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}


}


activity_maps.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/home_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:name="com.chilis.chilis.Maps" />

</RelativeLayout>
</LinearLayout>



Map.class


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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 Maps extends Fragment implements OnMapReadyCallback {

private GoogleMap mMap;
private FragmentActivity myContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.activity_maps, container, false);

SupportMapFragment mapFragment = (SupportMapFragment) myContext.getSupportFragmentManager() //error
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); //error

return rootView;
}

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

@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
}



AndroidManifest.xml


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

package="--------------">
<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="android.permission.ACCESS_COARSE_LOCATION" />
<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">
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

</application>



Answer

Check that in your activity_maps.xml file you are not missing the end tag of LinearLayout: </ LinearLayout>.

Comments