这是我的片段
public class HomeFragment extends Fragment implements LocationListener{
private GoogleMap map;
View rootView;
public Global global;
private Marker ubicacionActual;
private Marker destino;
private SeekBar tipoTaxi;
private ImageView taxy;
private ImageView car;
private ImageView deportivo;
private ImageView van;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
taxy = (ImageView)getActivity().findViewById(R.id.taxi);
car = (ImageView)getActivity().findViewById(R.id.car);
deportivo = (ImageView)getActivity().findViewById(R.id.deport);
van = (ImageView)getActivity().findViewById(R.id.van);
if(Global.flag ==0){
rootView = inflater.inflate(R.layout.fragment_home, null);
Global.v = rootView;
Global.flag=1;
try {
setUpMap();
} catch (InflateException e) {
}
}
else{
rootView = Global.v;
}
tipoTaxi = (SeekBar) rootView.findViewById(R.id.seekBar1);
tipoTaxi.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
seekBar.setProgress(progress);
if(progress == 0){
car.setVisibility(View.INVISIBLE);
deportivo.setVisibility(View.INVISIBLE);
van.setVisibility(View.INVISIBLE);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
return rootView;
}
private void setUpMap() {
// Get LocationManager object from System Service LOCATION_SERVICE
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//set map type
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setTrafficEnabled(true);
map.setMyLocationEnabled(true);
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(20));
ubicacionActual = map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Ubicacion Actual"));
ubicacionActual.showInfoWindow();
map.setOnMapClickListener(new OnMapClickListener(){
public void onMapClick(LatLng latLng) {
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
markerOptions.icon(bitmapDescriptor);
// Setting the title for the marker.
// This will be displayed on taping the marker
// markerOptions.title(latLng.latitude + " : " + latLng.longitude);
markerOptions.title("Destino");
// Clears the previously touched position
// Animating to the touched position
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
destino = map.addMarker(markerOptions);
destino.showInfoWindow();
map.setOnMapClickListener(null);
}
});
}
public void onLocationChanged(Location location) {
String str = "Latitude: "+location.getLatitude()+"Longitude: "+location.getLongitude();
Toast.makeText(getActivity(), str, Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
/******** Called when User off Gps *********/
Toast.makeText(getActivity(), "Gps apagado ", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
/******** Called when User on Gps *********/
Toast.makeText(getActivity(), "Gps prendido ", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@SuppressWarnings("unused")
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Tu GPS parece estar apagado para tener un mejor funcionamiento de la aplicacion te sugerimos encenderlo")
.setCancelable(false)
.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
当我在搜索栏进度为 1 时运行应用程序崩溃
原木猫
03-27 11:04:43.580: E/AndroidRuntime(9036): 致命异常: main 03-27 11:04:43.580: E/AndroidRuntime(9036): java.lang.NullPointerException 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 com.ionc.systax.HomeFragment$1.onProgressChanged(HomeFragment.java:106) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.widget.SeekBar.onProgressRefresh( SeekBar.java:92) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:700) 03-27 11:04:43.580: E/AndroidRuntime( 9036): 在 android.widget.ProgressBar.refreshProgress(ProgressBar.java:725) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.widget.ProgressBar.setProgress(ProgressBar.java:781) 03 -27 11:04:43.580: E/AndroidRuntime(9036): 在 android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:655) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:574) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在android.view.View.dispatchTouchEvent(View.java:7485) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2273) 03-27 11: 04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2273) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 03-27 11:04:43.580: E/AndroidRuntime (9036): 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2273) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2273) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 03-27 11: 04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2273) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2010) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2273) 03-27 11:04:43.580: E/AndroidRuntime (9036): 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2239) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1534) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.app.Activity .dispatchTouchEvent(Activity.java:2466) 03-27 11:04:43.580: E/AndroidRuntime(9036): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2187) 03- 27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.View.dispatchPointerEvent(View.java:7665) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view。 ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3813) 03-27 11:04:43.580:E/AndroidRuntime(9036): 在 android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3697) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java :4935) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4914) 03-27 11:04:43.580: E/AndroidRuntime(9036):在 android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5012) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 03- 27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.InputEventReceiver。consumeBatchedInputEvents(InputEventReceiver.java:174) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4985) 03-27 11:04:43.580: E/ AndroidRuntime(9036): 在 android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5031) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.Choreographer$CallbackRecord.run(Choreographer .java:791) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.Choreographer.doCallbacks(Choreographer.java:591) 03-27 11:04:43.580: E/AndroidRuntime(9036 ): 在 android.view.Choreographer.doFrame(Choreographer.java:559) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777) 03-27 11:04:43.580:E/AndroidRuntime(9036): 在 android.os.Handler.handleCallback(Handler.java:725) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.os.Handler.dispatchMessage(Handler.java :92) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 android.os.Looper.loop(Looper.java:137) 03-27 11:04:43.580: E/AndroidRuntime(9036):在 android.app.ActivityThread.main(ActivityThread.java:5306) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-27 11: 04:43.580: E/AndroidRuntime(9036): 在 java.lang.reflect.Method.invoke(Method.java:511) 03-27 11:04:43.580: E/AndroidRuntime(9036): 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 03-27 11:04:43.580: E/AndroidRuntime(9036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)03-27 11:04:43.580:E/AndroidRuntime(9036):在 dalvik.system.NativeStart.main(本机方法)
还有另一种使用搜索栏隐藏图像视图的方法吗?
对不起我的英语不好