我正在尝试在我的地图中使用谷歌地方。同时使用actionBar.setNavigationMode。我得到空指针异常,并且不推荐使用ActionBar类型的方法setNavigationMode(int)。我尝试将android min sdk版本更改为11并且几乎没有用。我试图根据不同的类型(基于zipcode)列出谷歌地图中的证书地点。在某些代码中,他们使用的是Fragment活动而不是活动。
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>
答案 0 :(得分:1)
由于在最新的API中不推荐使用actionBar.setNavigationMode方法,因此您将获得空指针异常。您可以使用SlidingTabLayout作为替代方案。有关更多信息,请查看以下链接以及如何使用SlidingTabs
的示例https://www.youtube.com/watch?v=Fl0xMuo10yA&index=26&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD
希望这有帮助。