zyonneo
zyonneo

Reputation: 1389

Why getting null pointer exception when using actionbar.setNavigationMode?

I am trying to use google places in my map.While using actionBar.setNavigationMode. I am getting null pointer exception and the method setNavigationMode(int) from the type ActionBar is deprecated. I tried changing the android min sdk version to 11 and all but no use. I am trying to list certaing places in google maps according to different types(based on zipcode). In some code they are using Fragment activity instead of activity.

package com.bar.start;
import java.util.ArrayList;
import com.bar.barapp.R;
import com.bar.location.Place;
import com.bar.location.PlacesService;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class LocationActivity extends Activity implements LocationListener {


     private final String TAG = getClass().getSimpleName();

     private String[] places;
     private LocationManager locationManager;
     private Location loc;

    GoogleMap map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        SharedPreferences sharedPreferences;
        int locationCount = 0;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.location);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

          //initCompo();
          places = getResources().getStringArray(R.array.places);
          //  currentLocation();
          final ActionBar actionBar = getActionBar();
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
          actionBar.setListNavigationCallbacks(ArrayAdapter.createFromResource(
                    this, R.array.places, android.R.layout.simple_list_item_1),
                    new ActionBar.OnNavigationListener() {

                        @Override
                        public boolean onNavigationItemSelected(int itemPosition,
                                long itemId) {
                            Log.e(TAG,
                                    places[itemPosition].toLowerCase().replace("-",
                                            "_"));
                            if (loc != null) {
                                map.clear();
                                new GetPlaces(LocationActivity.this,
                                        places[itemPosition].toLowerCase().replace(
                                                "-", "_").replace(" ", "_")).execute();
                            }
                            return true;
                        }
            });

    }

    @Override
    public void onLocationChanged(Location location) {

           map.clear();
           MarkerOptions mp = new MarkerOptions();
           mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
           mp.title("My Location");
           map.addMarker(mp);
           map.animateCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(location.getLatitude(), location.getLongitude()), 13));

    }

    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    public int getZoomLevel(Circle circle) {
        int zoomLevel = 1;
        if (circle != null){

            double radius = circle.getRadius();
            double scale = radius / 500;
            zoomLevel =(int) (16 - Math.log(scale) / Math.log(2));
        }
        return zoomLevel;
    }

    private class GetPlaces extends AsyncTask<Void, Void, ArrayList<Place>> {

        private ProgressDialog dialog;
        private Context context;
        private String places;

        public GetPlaces(Context context, String places) {
            this.context = context;
            this.places = places;
        }

        @Override
        protected void onPostExecute(ArrayList<Place> result) {
            super.onPostExecute(result);
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            for (int i = 0; i < result.size(); i++) {
                map.addMarker(new MarkerOptions()
                        .title(result.get(i).getName())
                        .position(
                                new LatLng(result.get(i).getLatitude(), result
                                        .get(i).getLongitude()))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.marker))
                        .snippet(result.get(i).getVicinity()));
            }
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(result.get(0).getLatitude(), result
                            .get(0).getLongitude())) // Sets the center of the map to
                                            // Mountain View
                    .zoom(14) // Sets the zoom
                    .tilt(30) // Sets the tilt of the camera to 30 degrees
                    .build(); // Creates a CameraPosition from the builder
            map.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(context);
            dialog.setCancelable(false);
            dialog.setMessage("Loading..");
            dialog.isIndeterminate();
            dialog.show();
        }

        @Override
        protected ArrayList<Place> doInBackground(Void... arg0) {
            PlacesService service = new PlacesService(
                    "API_key");
            ArrayList<Place> findPlaces = service.findPlaces(loc.getLatitude(), // 28.632808
                    loc.getLongitude(), places); // 77.218276

            for (int i = 0; i < findPlaces.size(); i++) {
                Place placeDetail = findPlaces.get(i);
                Log.e(TAG, "places : " + placeDetail.getName());
            }
            return findPlaces;
        }
    }
}

AndroidManifest.xml

    <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"/>

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


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
       >

        <activity android:name="com.bar.start.Startupscreen"
                  android:label="@string/app_name"
                   android:screenOrientation="portrait"
                     android:theme="@android:style/Theme">
                <intent-filter>
                   <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>

        </activity>

        <activity android:name="com.bar.start.MainActivity"
                  android:label="@string/app_name"
                   android:screenOrientation="portrait"
                    android:theme="@android:style/Theme">

        </activity>
        <activity android:name="com.bar.start.LocationActivity"
                  android:label="@string/app_name"
                   android:screenOrientation="portrait"
                    android:theme="@android:style/Theme">

        </activity>

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

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

    </application>

</manifest>

Upvotes: 0

Views: 413

Answers (1)

keshav kowshik
keshav kowshik

Reputation: 2374

You are getting null pointer exception since the method actionBar.setNavigationMode is deprecated in the latest API. You can use the SlidingTabLayout as an alternative. Check the following link for more information and an example of how to use SlidingTabs

https://www.youtube.com/watch?v=Fl0xMuo10yA&index=26&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD

Hope this helps.

Upvotes: 1

Related Questions