2013-09-27 69 views
1

我有一个问题,从util类实例化新对象到片段类。 实例化需要传递一个上下文,但我不能采取上下文。通过片段传递上下文

我试过传递getActivity(),但仍然得到一个GpsAssist.getLatitude的NULL POINTER EXCEPTION(因为这是第一个被调用的)错误。

我试图从GpsAssist

调用的方法getLatitude()和getLongitude()下面是我的代码。可以使用所有的帮助。

谢谢

MapsActivity类:

public class MapsActivity extends Fragment { 


GoogleMap map; 
private View view; 
GpsAssist gAssist = new GpsAssist(getActivity()); 
double lat = gAssist.getLatitude(); 
double lng = gAssist.getLongitude(); 


private final LatLng MOUNTAIN_VIEW = new LatLng(lat, lng); 

private LatLngBounds loc = new LatLngBounds(new LatLng(37, -121), new LatLng(37,-121)); 

public void get(){ 


} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 



    View view = inflater.inflate(R.layout.map_test, container, false); 

    if(map == null){ 
     map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
     map.addMarker(new MarkerOptions().position(MOUNTAIN_VIEW).title("Marker")); 
     map.animateCamera(CameraUpdateFactory.newCameraPosition(posi())); 

    }else{ 

     //map.moveCamera(CameraUpdateFactory.newLatLngBounds(loc, 15)); 

    } 


    return view; 
} 
public CameraPosition posi(){ 
    CameraPosition posi = new CameraPosition.Builder() 
    .target(MOUNTAIN_VIEW) 
    .zoom(17) 
    .build(); 
    return posi; 
} 




@Override 
public void onDestroyView() { 
     try{ 
      SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)); 
       FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
       ft.remove(fragment); 
       ft.commit(); 
       //map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
       }catch(Exception e){ 
       } 
      super.onDestroyView(); 
} 







} 

我GpsAssist类:

public class GpsAssist extends Service { 


//Primitive declarations 
private double lat; 
private double lng; 
private String provider; 

//Class instances 
private Context context; 
private Geocoder gCoder; 
Location location; 


public GpsAssist(Context context){ 

    this.context = context; 
} 


//Checking if GPS is enabled 

LocationManager lManager; 

public Location gpsConnected(){ 

    lManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

    boolean gpsEnabled = lManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    boolean networkEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 



    if(! gpsEnabled && ! networkEnabled){ 

    Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    startActivity(i); 
    }else { 


     location = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 



     if(location != null){ 

      lat = getLatitude(); 
      lng = getLongitude(); 

      Toast t = Toast.makeText(context, "Your location" + lat + " " + lng, Toast.LENGTH_SHORT); 
      t.show(); 

      String addresses = getAddress(lat, lng, 2); 
      Toast address = Toast.makeText(context, addresses, Toast.LENGTH_LONG); 
      address.show(); 


     }else{ 

      Toast t = Toast.makeText(context, "Cannot get your location", Toast.LENGTH_SHORT); 
      t.show(); 
     } 

    } 
    return location; 
} 

//Method to find the best possible provider 
public String getProvider(){ 

    Criteria criteria = new Criteria(); 
    //true means that only enabled providers will be returned 
    provider = lManager.getBestProvider(criteria, true); 

    return provider; 
} 

public double getLatitude(){ 
    lat = location.getLatitude(); 
    return lat; 
} 

public double getLongitude(){ 
    lng = location.getLongitude(); 
    return lng; 
} 

public String getAddress(double lat, double lng, int maxR){ 

    String addres = ""; 
    gCoder = new Geocoder(context, Locale.getDefault()); 
    try { 
    List<Address> address = gCoder.getFromLocation(lat, lng, maxR); 

     if(address != null){ 

      Address adName = address.get(0); 

      StringBuilder str = new StringBuilder("Address: \n"); 
      for(int i=0; i<adName.getMaxAddressLineIndex(); i++){ 
       str.append(adName.getAddressLine(i)).append("\n"); 
      } 
      addres = str.toString(); 
      System.out.println(addres); 
     }else{ 

      addres = "Address is null"; 
     } 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

回答

2

的服务,您可以只使用this的背景下,你不需要给它的上下文。

您还在使用错误的服务。服务不是通过创建新对象来启动的

+0

+1使我了解到 – HpTerm

+0

当我试图将所有上下文更改为this并删除构造函数,它给了我一个错误的主要活动是“ContextWrapper.getSystemServices空指针异常 – user2510952

+0

我将如何改变GpsAssist类正确实施getSystemservice()从getSystemService – user2510952

0

查看您的GpsAssist类的代码,getLatitude()和getLongitude()引用位置实例变量,它看起来像只由gpsConnected( )电话。由于您尚未调用gpsConnected(),因此位置将为空,从而导致您的空指针异常。您可以在您的onCreate中调用gpsConnected(),并在调用gpsConnected()后调用getLatitude()和getLongitude()