Question

I'm new to android. I built an application in which there is a Button which starts an Activity and there are two more Buttons in that Activity which will open two seperate activities. One of that Activity contains Google map named as nearby search. When I start the nearby search the app is crashing while this Activity was running perfectly before integrating the map.

Here is the log cat

04-02 02:32:40.354: E/AndroidRuntime(22037): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jamaat_times/com.example.jamaattiming.NearbySearch}: java.lang.NullPointerException
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.os.Handler.dispatchMessage(Handler.java:107)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.os.Looper.loop(Looper.java:194)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.ActivityThread.main(ActivityThread.java:5371)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at java.lang.reflect.Method.invoke(Method.java:525)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at dalvik.system.NativeStart.main(Native Method)
04-02 02:32:40.354: E/AndroidRuntime(22037): Caused by: java.lang.NullPointerException
04-02 02:32:40.354: E/AndroidRuntime(22037):    at com.example.jamaattiming.NearbySearch.onCreate(NearbySearch.java:36)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.Activity.performCreate(Activity.java:5122)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-02 02:32:40.354: E/AndroidRuntime(22037):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-02 02:32:40.354: E/AndroidRuntime(22037):    ... 11 more

here is the java file:

    public class NearbySearch extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby_search);
        GoogleMapOptions mapOptions = new GoogleMapOptions();
         GoogleMap maps=(((MapFragment) getFragmentManager().findFragmentById(R.id.map2)).getMap());

         mapOptions.mapType(GoogleMap.MAP_TYPE_HYBRID);
         //maps.setMapType(GoogleMap.MAP_TYPE_HYBRID);
         maps.setMyLocationEnabled(true);
         maps.addMarker(new MarkerOptions()
            .position(new LatLng(24.9967 , 66.1234))
            .title("Hello world"));
    }

}

here is the xml file:

<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"
    android:orientation="vertical" 
    android:background="#808080">

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

</RelativeLayout>

and here is the manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jamaat_times"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_jamaat"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.jamaattiming.Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.jamaattiming.MainPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.CLEARSCREEN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.jamaattiming.Qibla"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.example.COMPASS" />

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

        <activity
            android:name="com.example.jamaattiming.JamaatFinder"
            android:label="@string/title_activity_jamaat_finder" >
        </activity>

        <activity
            android:name="com.example.jamaattiming.QiblaFinder"
            android:label="@string/title_activity_qibla_finder" >
        </activity>

        <activity
            android:name="com.example.jamaattiming.TagYourself"
            android:label="@string/title_activity_tag_yourself" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.example.jamaattiming.NearbySearch"
            android:label="@string/title_activity_nearby_search" >
        </activity>


        <activity
            android:name="com.example.jamaattiming.ManualSearch"
            android:label="@string/title_activity_manual_search" >
        </activity>
    </application>

</manifest>
Was it helpful?

Solution

You need to change this

<uses-sdk
  android:minSdkVersion="8"

to

<uses-sdk
  android:minSdkVersion="12"

as you should using MapFragment from api level above 11

It looks like GooleMap object maps is null. It is better to check the Availability of google play services before initializing map object.

Look at Check for Google Play Services

http://developer.android.com/training/location/retrieve-current.html

Follow

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

public final GoogleMap getMap ()

Gets the underlying GoogleMap that is tied to the view wrapped by this fragment.

Returns the GoogleMap. Null if the view of the fragment is not yet ready. This can happen if the fragment lifecyle have not gone through onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also happen if Google Play services is not available. If Google Play services becomes available afterwards and the fragment have gone through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this method again will initialize and return the GoogleMap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top