嘿我正在尝试将goole maps片段实现到制表符片段。 tabhost也是片段活动中的另一个片段,带有导航抽屉这里是我当前的代码我在地图初始化时得到一个空指针异常
package com.shimba.android.secure.fragments;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.Polyline;
import com.shimba.android.secure.R;
import com.shimba.android.secure.http.NetworkConnectionStatus;
import com.shimba.android.secure.http.RestClient;
public class LocationFragment extends Fragment
implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener,
OnMyLocationButtonClickListener,OnMarkerClickListener {
private GoogleMap map;
private static View v;
private LocationClient mLocationClient;
Location location;
double longitude;
double latitude;
Polyline polyLine;
protected LatLng start;
String distance, duration;
Marker marker2;
protected LatLng end;
private SharedPreferences prefs;
ArrayList<Marker> mArrayMarkers;
List<Double> listDistance;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
if (v != null) {
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);
}
try {
v = inflater.inflate(R.layout.fragment_location, container, false);
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true);
map.setOnMarkerClickListener(this);
// if (NetworkConnectionStatus.isOnline(getActivity())) {
// new GetPlacesTask().execute();
//}
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return v;
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
mLocationClient.connect();
}
@Override
public void onPause() {
super.onPause();
if (mLocationClient != null) {
mLocationClient.disconnect();
}
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (map == null) {
// Try to obtain the map from the SupportMapFragment.
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (map != null) {
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
}
}
}
private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(
getActivity(),
this, // ConnectionCallbacks
this); // OnConnectionFailedListener
}
}
public void showMyLocation(View view) {
if (mLocationClient != null && mLocationClient.isConnected()) {
String msg = "Location = " + mLocationClient.getLastLocation();
Toast.makeText(getActivity(), "hating me", 155000).show();
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onDisconnected() {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
@Override
public boolean onMyLocationButtonClick() {
return false;
}
public class GetPlacesTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String url = null;
try {
url = "https://maps.googleapis.com/maps/api/place/search/json?location=%20-1.296436,36.791540&keyword=safaricom+shop&rankby=distance&sensor=false&key=AIzaSyBVwYvGApeZmrPCTilzMKgpk5Hr48TisbM";
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String json = "{}";
try {
json = RestClient.makeRestRequest(RestClient.GET, url, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Editor editor = prefs.edit();
editor.putString("ShopPlaces", json);
editor.commit();
return json;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//System.out.println(result);
}
}
}
非常感谢任何帮助
错误日志看起来像这样
java.lang.NullPointerException
at com.shimba.android.secure.fragments.LocationFragment.onCreateView(LocationFragment.java:92)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:11799)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2417)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2424)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1233)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1029)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4356)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4747)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)