This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Hi I am trying to create a app where I am will be displaying map in a fragment using SupportMapFragment but I am getting NullPointerException. Here is the full error code
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at obs.sharemytable.fragment.Search.onCreateView(Search.java:49)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6918)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Search.java:
public class Search extends Fragment{
GoogleMap mGoogleMap;
public static Search newInstance() {
Search fragment = new Search();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.search_layout, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map_container);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mGoogleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
});
return view;
}
}
search_layout.xml
<?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="#android:color/white">
<include
android:id="#+id/searchToolbar"
layout="#layout/toolbar" />
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>
</LinearLayout>
Can someone figure what mistake I have done here.
Try getting the FragmentManager from the activity and using it to get your map:
FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();
final SupportMapFragment mapFragment = (SupportMapFragment) mFragmentManager
.findFragmentById(R.id.map_container);
SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().fragmentManager.findFragmentById(R.id.location_map))
Thanks #MojioMS now its working for me. Here is the changes I made to work.
Change this search_layout.xml
<?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="#android:color/white">
<include
android:id="#+id/searchToolbar"
layout="#layout/toolbar" />
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>
</LinearLayout>
to
<?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="#android:color/white">
<include
android:id="#+id/searchToolbar"
layout="#layout/toolbar" />
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</LinearLayout>
When you use delay after initializing SupportMapFragment then there will not null pointer exception.
Related
I have created a standard Google Maps Activity in my app in Android Studio and the map works as expected.
My Activity is called 'NativeMap' and then the activity starts I see a ToolBar/AppBar at the top of the activity with the text "NativeMap".
The layout file for the activity is standard and calls a maps fragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NativeMap">
</androidx.fragment.app.FragmentContainerView>
As such, there is no toolbar defined in the XML for me to reference or customize...
The onCreate for this activity seems very simple as well, so again I can only assume that there is a lot of stuff included in the background.
// public class NativeMap extends FragmentActivity implements OnMapReadyCallback {
public class NativeMap extends AppCompatActivity implements OnMapReadyCallback {
private static final String TAG = NativeMap.class.getSimpleName();
private GoogleMap mMap;
private ActivityNativeMapBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityNativeMapBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) mapFragment.getMapAsync(this);
}
#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));
}
}
(Note that I have switched this class to extend AppCompatActivity to get the bar to appear at the top, when it extends FragmentActivity no top bar appears at all.)
How can I access this toolbar or replace it with a standard toolbar.
I also want to add a drawer menu to this activity, but everything looks so abstracted via this fragment inclusion that I have no idea how to.
I've been Googling for about a week now and can't find anything that even seems to point me in the right direction with this.
I figured it out.
androidx.fragment.app.FragmentContainerView can only contain fragments, so the base container in the layout needed to be changed.
I changed it to a <androidx.drawerlayout.widget.DrawerLayout to allow the main menu drawer to be included. (called "_drawer" in the tree view below)
Inside the DrawerLayout I added a androidx.constraintlayout.widget.ConstraintLayout to contain the main layout components, including the map fragment and the toolbar. (The contraint layout is called "mainLayout" in the treeview below)
The toolbar was then added inside a com.google.android.material.appbar.AppBarLayout.
The map fragment was then added in a <fragment> tag below the AppBarLayout.
This works as expected.
The layout XML is now as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/_drawer"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/mainLayout"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/top_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_logo"
app:srcCompat="#drawable/ic_app_top_logo" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
tools:context=".NativeMap" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="#+id/_nav_view"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#EEEEEE">
<include
android:id="#+id/drawerMenu"
layout="#layout/drawer_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
I’m trying to use a tabbed activity within a fragment, so a fragment with two other fragments. I keep getting the NullPointerException for the viewPager object, which has already been mapped by the right id. Whats gone wrong ?
public class BookFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_books, container, false);
PageAdapter pageAdapter = new PageAdapter(getChildFragmentManager(), 1);
pageAdapter.addFragment("Pending", new PendingFragment());
pageAdapter.addFragment("Scheduled", new ScheduledFragment());
ViewPager viewPager = view.findViewById(R.id.pager_books);
viewPager.setAdapter(pageAdapter);
TabLayout tabs = view.findViewById(R.id.book_tabs);
tabs.setupWithViewPager(viewPager);
return view;
}
}
The fragment Layout (fragment_books.xml) being inflated;
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".views.bookhub_navigation.ui_fragments.books.BookFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout
android:id="#+id/book_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:id="#+id/pending_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pending" />
<com.google.android.material.tabs.TabItem
android:id="#+id/scheduled_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scheduled" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager_books"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
It keeps giving a NullPointerException when the id pager_books is in the right view heirarchy which is the fragment_books.xml file.
The error log
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)' on a null object reference at com.immers_ltd.views.bookhub_navigation.ui_fragments.books.BookFragment.onCreateView(DateFragment.java:58) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
I have created a new Android project with a Google Map activity defined as
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);
}
The Layout is this
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alewe.myapplication.MapsActivity" />
The problem is :
i have created another activity and im trying to add the google map fragment inside it.
Using the
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.firstLayout,this,this.getTag());
doesnt work because i cant cast FragmentActivity to Fragment.
How can i add the google maps fragment just created inside another activity ?
Thanks
Change your layout (activity_maps) like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alewe.myapplication.MapsActivity"
android:id="#+id/main_view_map">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
app:fabSize="mini"
app:srcCompat="#drawable/ic_refresh_black_24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
And don't use
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.firstLayout,this,this.getTag());
this code.
Problem with this, This code is also not working..
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
mapView = (MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
});
I found a solution today.
Implement OnMapReadyCallback to MainActivity.
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener,
OnMapReadyCallback
{
// ...
}
Implement onMapReady(). Keep it empty.
#Override
public void onMapReady(GoogleMap googleMap)
{
}
In onCreate() method:
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
You are mixing up two processes. You have to change either in your xml or activity class code:
1) Either replace your map fragment in content_main.xml with the MapView (As Below):
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2) Or replace this:
mapView = (MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
});
With:
((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
}
In your MainActivity class.
In your existing code, You have added a map fragment to the layout but calling for a mapview from activity. That's why it is not working.
To get API key, go HERE and Generate a key.
Copy the key and paste it in manifest under application tag:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
Now the google map will show up.
first you have to create the relative_layout because we can't add anything in map fragment so we add image button and map fragment in relative_layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/activity_maps"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
here your drawer_layout
`<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/activity_maps"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>`
in MapActivity add implements method navigationView and then add this code below the getSupportedFragment
` DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);`
then write the onclick method on Imagebutton method which is ` public void
drawerclick(View view) {
drawer.openDrawer(Gravity.START);
enter code here
navigationView.setNavigationItemSelectedListener(this);
}`
We have different differnet activities of google map ( Some with tabs, Some simple, And some other activities), So we are are trying to implement navigation drawer in our app, For this we created a class which extend ActionBarActivity and has drawer, And we are extending this main activity in our classes for navigation drawer, But when we open our application its showing map is null.
Please help.
Here is our files
drawerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FA0"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/shitstuff"
app:itemTextColor="#000"
app:menu="#layout/drawermenu"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
mapload.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffc17540"
android:id="#+id/maptab"
>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:gravity="bottom"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:id="#+id/slidingContainer"
android:layout_width="fill_parent"
android:gravity="bottom"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/t"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffff773c"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/rg_views"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton
android:id="#+id/rb_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/str_rb_normal"
android:checked="true" />
<RadioButton
android:id="#+id/rb_satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/str_rb_satellite" />
<RadioButton
android:id="#+id/rb_terrain"
android:layout_width="161dp"
android:layout_height="wrap_content"
android:text="#string/str_rb_terrain" />
</RadioGroup>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/inhorizontalscrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#android:drawable/ic_input_add" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<View
android:id="#+id/transparentView"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="#dimen/map_height"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>
BaseActivity.java
public class BaseActivity extends ActionBarActivity
{
public DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_list);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// You were missing this setHomeAsUpIndicator
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_navigation_drawer);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView n = (NavigationView) findViewById(R.id.shitstuff);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout , R.string.drawer_open, R.string.drawer_close)
{
/*
* Called when a drawer has settled in a completely closed state
*/
public void onDrawerClosed(View view)
{
Log.d("drawerToggle", "Drawer closed");
super.onDrawerClosed(view);
// getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
}
/*
* Called when a drawer has settled in a completely open state
*/
public void onDrawerOpened(View drawerView)
{
Log.d("drawerToggle", "Drawer opened");
super.onDrawerOpened(drawerView);
// getActionBar().setTitle("NavigationDrawer");
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
////.......
}
mDrawerLayout.closeDrawers(); // CLOSE DRAWER
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// THIS IS YOUR DRAWER/HAMBURGER BUTTON
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
}
Mylocation.java
public class MyLocation extends BaseActivity implements View.OnClickListener,OnMapReadyCallback {
GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mapload, null, false); // line no-74
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
But everytime i run application i am getting this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{MyLocation}: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74) //error here
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74) //error here
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
What I tried:-
inflater.from(getApplicationContext()).inflate(R.layout.mapload, pageHolder, true);
GoogleMap mapFragment =((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); //getting null pointer exception here
another thing which I tried
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapFragment).commit(); //same null pointer exception
The line with inflater.inflate is doing nothing useful there in that class. The XML layout isn't loaded and findFragmentById(R.id.map) is returning your null.
I think you wanted something like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.mapload);
// Find fragment here
}
Or maybe you maybe you just wanted setContentView from the parent class?
Basically, the error is saying your <fragment> tag is missing a class.
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
<!-- Needs a class or name attribute -->
android:gravity="bottom"
android:layout_height="fill_parent" />
For example, from the documentation
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
(Also worth mentioning that xmlns:android only is to be applied to the root xml element)
Though, I do not think you need a whole separate Activity or layout.
Load the map fragment into this FrameLayout
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
just like any regular Fragment...
SupportMapFragment mapFragment = new SupportMapFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content, mapFragment)
.commit();
mapFragment.getMapAsync(this);
add this line to your xml:
android:name="com.google.android.gms.maps.SupportMapFragment"
like this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:gravity="bottom"
android:layout_height="fill_parent"/>
and the best way to implement google map with drawer is to use fragment, that's mean change your class "MyLocation" to extends from fragment