2016-09-22 74 views
0

[![输入图像描述] [1]] [1]你好,我有一个问题任何人都可以告诉我如何解决这个问题,我想在片段类中使用constructer: 这里是我的两个类 AppLocationService:如何将java类的构造函数转换为fragment?

public class AppLocationService extends Service implements LocationListener { 

    protected LocationManager locationManager; 
    Location location; 

    private static final long MIN_DISTANCE_FOR_UPDATE = 10; 
    private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2; 

    public AppLocationService(Context context) { 
     locationManager = (LocationManager) context 
       .getSystemService(LOCATION_SERVICE); 
    } 

    public Location getLocation(String provider) { 
     if (locationManager.isProviderEnabled(provider)) { 
      locationManager.requestLocationUpdates(provider, 
        MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this); 
      if (locationManager != null) { 
       location = locationManager.getLastKnownLocation(provider); 
       return location; 
      } 
     } 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

} 

这里是片段:

public class AddressFragment extends Fragment { 
    Button ShowAddress; 
    TextView tvAddress; 

    AppLocationService appLocationService; 
    public AddressFragment(){ 

    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_address, container, false); 
     tvAddress = (TextView) rootView.findViewById(R.id.input_location); 
     appLocationService = new AppLocationService(
       AddressFragment.this); 


     ShowAddress = (Button) rootView.findViewById(R.id.btn_location); 
     ShowAddress.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 

       Location location = appLocationService 
         .getLocation(LocationManager.GPS_PROVIDER); 

       //you can hard-code the lat & long if you have issues with getting it 
       //remove the below if-condition and use the following couple of lines 
       //double latitude = 37.422005; 
       //double longitude = -122.084095 

       if (location != null) { 
        double latitude = location.getLatitude(); 
        double longitude = location.getLongitude(); 
        LocationAddress locationAddress = new LocationAddress(); 
        locationAddress.getAddressFromLocation(latitude, longitude, 
          getApplicationContext(), new GeocoderHandler()); 
       } else { 
        showSettingsAlert(); 
       } 

      } 
     }); 
     return rootView; 
    } 




    public void showSettingsAlert() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(
       AddressFragment.this); 
     alertDialog.setTitle("SETTINGS"); 
     alertDialog.setMessage("Enable Location Provider! Go to settings menu?"); 
     alertDialog.setPositiveButton("Settings", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(
           Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
         AddressFragment.this.startActivity(intent); 
        } 
       }); 
     alertDialog.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 
     alertDialog.show(); 
    } 

    private class GeocoderHandler extends Handler { 
     @Override 
     public void handleMessage(Message message) { 
      String locationAddress; 
      switch (message.what) { 
       case 1: 
        Bundle bundle = message.getData(); 
        locationAddress = bundle.getString("address"); 
        break; 
       default: 
        locationAddress = null; 
      } 
      tvAddress.setText(locationAddress); 
     } 
    } 




     // Inflate the layout for this fragment 



    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 
} 
+0

变化这在onCreateView methord您的片段: appLocationService =新AppLocationService( getActivity()); –

+0

设置appLocationService =新AppLocationService(getApplicationContext()); –

回答

0

要回答你的问题; A Fragment并非来自Context。为了让你的代码编译,你应该改变这种

appLocationService = new AppLocationService(
      AddressFragment.this); 

对此

appLocationService = new AppLocationService(getActivity()); 

但是,你永远不应该叫一个Service自己的构造函数。你应该Context#startService(Intent)(在你的情况getActivity().startService(new Intent(getActivity(), AppLocationService.class))启动它要为您的服务提供任何参数,看看Intent#putExtra(String, String)

+0

嗨我做了我修复那一个,但现在它显示错误:(67,29)错误:找不到符号方法getApplicationContext() – help

+1

我没有提到任何关于'getApplicationContext ()''''''''''''''现在你犯的错误和以前一样,你可以从上下文派生类调用'getApplicationContext()',(正如我所说)片段不是 – 0xDEADC0DE

+0

好吧,请告诉我必须做什么? – help

相关问题