1 Replies - 11604 Views - Last Post: 21 May 2015 - 08:31 AM

#1 theboxmike   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-September 12

Getting a null object reference?

Posted 19 May 2015 - 09:36 PM

Hi everyone,

I'm getting a null object reference I have no idea why, this problem has been giving my grief for a while: Here's what the error log says:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.blahblah.blahblah/com.examples.blahblah.blahblah.MainActivity2Activity}: 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


Basically what i'm trying to do is add a GoogleMaps inside one of my fragments, however it seems like every time I fix something, something else breaks.
Now I've tried adding the supportmanager to my project however this doesn't seem to have fixed the main issue of not being unable to start the activity and referencing a null object. So i'm going to post my MainActivity and my Fragment to see if anybody has any idea as to why this is occurring.

MainActivity:

    import android.app.ActionBar;
    import android.app.Activity;
    import android.support.v4.app.FragmentManager;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.widget.DrawerLayout;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.support.v4.app.Fragment;
    
    
    
    /* public class MainActivity extends FragmentActivity {
    
       public static FragmentManager fragmentManager;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
    
           // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.
           fragmentManager = getSupportFragmentManager();
       }
    } */
    
    
    public class MainActivity2Activity extends FragmentActivity
            implements NavigationDrawerFragment.NavigationDrawerCallbacks {
    
        /**
         * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
         */
        private NavigationDrawerFragment mNavigationDrawerFragment;
    
        /**
         * Used to store the last screen title. For use in {@link #restoreActionBar()}.
         */
        private CharSequence mTitle;
    
        public static android.support.v4.app.FragmentManager fragmentManager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_activity2);
    
    
    
    
            // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.
            fragmentManager = getSupportFragmentManager();
    
    
            mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getFragmentManager().findFragmentById(R.id.navigation_drawer);
            mTitle = getTitle();
    
            // Set up the drawer.
            mNavigationDrawerFragment.setUp(
                    R.id.navigation_drawer,
                    (DrawerLayout) findViewById(R.id.drawer_layout));
    
            // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.
            fragmentManager = getSupportFragmentManager();
    
    
        }
    
        @Override
        public void onNavigationDrawerItemSelected(int position) {
    
            // This method controls the navigation drawer
    
            Fragment objFragment = null;
    
            switch (position) {
                case 0:
                    objFragment = new menu_1_fragment();
                    break;
                case 1:
                    objFragment = new menu_2_fragment();
                    break;
                case 2:
                    objFragment = new menu_3_fragment();
                    break;
                case 3:
                    objFragment = new menu_4_fragment();
                    break;
                case 4:
                    objFragment = new menu_5_fragment();
                    break;
            }
    
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, objFragment)
                    .commit();
        }
    
        public void onSectionAttached(int number) {
            switch (number) {
                case 1:
                    mTitle = getString(R.string.title_section1);
                    break;
                case 2:
                    mTitle = getString(R.string.title_section2);
                    break;
                case 3:
                    mTitle = getString(R.string.title_section3);
                    break;
                case 4:
                    mTitle = getString(R.string.title_section4);
                    break;
                case 5:
                    mTitle = getString(R.string.title_section5);
                    break;
            }
        }
    
        public void restoreActionBar() {
            ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            actionBar.setDisplayShowTitleEnabled(true);
            // actionBar.setTitle(mTitle);
        }
    
    
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            if (!mNavigationDrawerFragment.isDrawerOpen()) {
                // Only show items in the action bar relevant to this screen
                // if the drawer is not showing. Otherwise, let the drawer
                // decide what to show in the action bar.
                getMenuInflater().inflate(R.menu.main_activity2, menu);
                restoreActionBar();
                return true;
            }
            return super.onCreateOptionsMenu(menu);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            // Link to settings in the main menu or create google maps options
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {
            /**
             * The fragment argument representing the section number for this
             * fragment.
             */
            private static final String ARG_SECTION_NUMBER = "section_number";
    
            /**
             * Returns a new instance of this fragment for the given section
             * number.
             */
            public static PlaceholderFragment newInstance(int sectionNumber) {
                PlaceholderFragment fragment = new PlaceholderFragment();
                Bundle args = new Bundle();
                args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                fragment.setArguments(args);
                return fragment;
            }
    
            public PlaceholderFragment() {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main_activity2, container, false);
                return rootView;
            }
    
    
            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                ((MainActivity2Activity) activity).onSectionAttached(
                        getArguments().getInt(ARG_SECTION_NUMBER));
            }
        }
    
    }


and my Fragment:



   import android.os.Bundle;
    import android.support.v4.app.Fragment;
    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 menu_1_fragment extends Fragment implements OnMapReadyCallback {
    
            View rootview;
    
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                rootview = inflater.inflate(R.layout.menu1_layout, container, false);
    
    
                return rootview;
            }
    
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
                mapFragment.getMapAsync(this);
            }
    
            @Override
            public void onMapReady(GoogleMap map) {
                LatLng sydney = new LatLng(-33.867, 151.206);
    
                map.setMyLocationEnabled(true);
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
    
                map.addMarker(new MarkerOptions()
                        .title("Sydney")
                        .snippet("The most populous city in Australia.")
                        .position(sydney));
            }
        }



And just incase somebody things there might be an issue with my XML layout file:



    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.MapFragment"/>
    
    
    
       <!-- <com.google.android.gms.maps.MapView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/map" /> -->
    
    
    
      <!--  <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" /> -->
    
    
    
    </RelativeLayout>



Is This A Good Question/Topic? 0
  • +

Replies To: Getting a null object reference?

#2 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Getting a null object reference?

Posted 21 May 2015 - 08:31 AM

Can you please provide the complete error message from the logcat. Also when posting multiple code files, kindly do mention what the file names are. e.g. MainActivity.java or layout_map.xml.

29
	             SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));

Try moving the above code in your Fragment class to the onCreateView method. Instead try this code,
mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));


Hope it Helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1