0

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
pankaj agarwal
  • 171
  • 2
  • 15

2 Answers2

0

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);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks for your reply. I added class in fragment and also use setcontentview. So my new code is http://pastebin.com/aSw9DXww But now i am getting this error java.lang.IllegalArgumentException: No view found for id 0x7f0d0093 (com.keepAeye.gps:id/content) for fragment SupportMapFragment{b182300 #1 id=0x7f0d0093} – pankaj agarwal Dec 22 '16 at 19:34
  • 2
    You do not need `setContentView(R.layout.mapload)` or if you do, then you are missing `@id/content`, yes. Just look at your XML files. `drawerlist.xml` is where that `FrameLayout` with the ID is located, not in `mapload.xml` – OneCricketeer Dec 22 '16 at 19:38
  • 1
    In other words, if you `setContentView` on `MyLocation`, you lose the entire NavigationView. – OneCricketeer Dec 22 '16 at 19:39
  • Awesome, I was trying to solve this from past 2 days. Thanks, It's working. but I have one more query, As you can see i have radio buttons and other attributes on mapload.xml, So if if i cant use setContentView, Then how I can access those attributes? – pankaj agarwal Dec 22 '16 at 19:48
  • Means, How we can access layout specific attributes? – pankaj agarwal Dec 22 '16 at 20:00
  • All I am saying is that a whole separate Activity will have a different layout. You don't want to `extends BaseActivity` if you want to keep the radio buttons and display `mapload.xml`. You would need a Fragment if you want to keep the NavigationDrawer – OneCricketeer Dec 22 '16 at 21:20
  • I Think this one is correct http://stackoverflow.com/questions/22137283/extending-navigation-drawer-activity-to-other-activities – pankaj agarwal Dec 23 '16 at 09:13
  • Okay, sure, but I don't really understand why you need a BaseActivity class that is limiting yourself to always have a NavigationView. If you want to only show the map and the activity with the drawer, you do not need a separate Activity class. You put the map Fragment into the existing FrameLayout of the BaseActivity, and that's it – OneCricketeer Dec 23 '16 at 13:50
  • No, I want to show map with some input box, some content and some attribute with map, And want to pass those data to another screen, So i think i need activity, Thats the problem. – pankaj agarwal Dec 28 '16 at 16:49
  • Yes, you always need "an Activity", I just don't know what you need to "extend BaseActivity" – OneCricketeer Dec 29 '16 at 00:50
0

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

Yessine Mahdouani
  • 1,225
  • 12
  • 24