我有一个使用位置管理器的片段,但是它出现了这个错误:
03-10 17:40:59.875 3909-3909/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.beerportfolio.beerportfoliopro.FindBrewery.<init>(FindBrewery.java:25)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
at com.beerportfolio.beerportfoliopro.MainDrawer2.displayNavFragment(MainDrawer2.java:172)
at com.beerportfolio.beerportfoliopro.MainDrawer2.access$000(MainDrawer2.java:28)
at com.beerportfolio.beerportfoliopro.MainDrawer2$1.onItemClick(MainDrawer2.java:71)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1237)
at android.widget.ListView.performItemClick(ListView.java:4555)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3037)
at android.widget.AbsListView$1.run(AbsListView.java:3724)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5789)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
at dalvik.system.NativeStart.main(Native Method)
FindBrewery第25行是这样的:
LocationManager locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
FindBrewery的整个代码是:
public class FindBrewery extends Fragment implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
LocationManager locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
private String provider;
Context c = getActivity().getApplicationContext();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//todo change view
View rootView = inflater.inflate(R.layout.beer_location_list,container, false);
// Get the location manager
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
}
return rootView;
}
/* Request updates at startup */
@Override
public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
//latituteField.setText(String.valueOf(lat));
//longitudeField.setText(String.valueOf(lng));
//Toast.makeText(this, "Finding your loaction",Toast.LENGTH_SHORT).show();
//call asycn task for location
String url = "myURL";
Log.d("urlTest", url);
//async task goes here
new GetNearbyBreweries(this.getActivity()).execute(url);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
//Toast.makeText(this, "Enabled new provider " + provider,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
//Toast.makeText(this, "Disabled provider " + provider,Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:2)
getActivity()
将返回null
,直到Fragment
附加到Activity
。您应该将代码移至onAttach()
:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
locationManager =(LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
}