无法在Android中的Dialog类中添加一些覆盖方法

我自横刀向天笑,去留肝胆两昆仑。这篇文章主要讲述无法在Android中的Dialog类中添加一些覆盖方法相关的知识,希望能为你提供帮助。
我创建了一个扩展Dialog类的类。在本课程中,我想在此课程中添加“位置更改”代码。当我尝试做同样的事情时,它向我展示了2个问题

  1. 一个是方法不会覆盖它超类
  2. 错误的第一个参数类型:找到:'android.context.Context',必需:'android.support.v4.app.FragmentActivity'
现在代码相同
public class DialogChooseYourLocation extends Dialog implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {private Location mylocation; private GoogleApiClient googleApiClient; private final static int REQUEST_CHECK_SETTINGS_GPS = 0x1; private final static int REQUEST_ID_MULTIPLE_PERMISSIONS = 0x2; Double latitude; Double longitude; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setUpGClient(); }

//在这里得到错误:错误的第一个参数类型:找到:'android.context.Context',必需:'android.support.v4.app.FragmentActivity'
private synchronized void setUpGClient() { googleApiClient = new GoogleApiClient.Builder(context) .enableAutoManage(getContext(), 0, this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); googleApiClient.connect(); }

//获取错误:一个方法不会覆盖超类
@Override public void onLocationChanged(Location location) { mylocation = location; if (mylocation != null) { latitude = mylocation.getLatitude(); longitude = mylocation.getLongitude(); Log.e("TAG", "onLocationChanged: " + "Latitude : " + latitude + "Longitude : " + longitude); GeoLocation(); //Or Do whatever you want with your location } }@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CHECK_SETTINGS_GPS: switch (resultCode) { case Activity.RESULT_OK: getMyLocation(); break; case Activity.RESULT_CANCELED: ((Activity) context).finish(); break; } break; } }private void checkPermissions() { int permissionLocation = ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION); List< String> listPermissionsNeeded = new ArrayList< > (); if (permissionLocation != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION); if (!listPermissionsNeeded.isEmpty()) { ActivityCompat.requestPermissions(getOwnerActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS); } } else { getMyLocation(); }}

【无法在Android中的Dialog类中添加一些覆盖方法】//获取错误:一个方法不会覆盖超类
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { int permissionLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION); if (permissionLocation == PackageManager.PERMISSION_GRANTED) { getMyLocation(); } }

}
答案在enableAutoManage方法错误的第一个参数类型: - 此方法需要第一个参数作为FragmentActivit的引用,但是您已经传递了Context的引用。所以有两个解决方案。您必须将FragmentActivity扩展到您的类DialogChooseYourLocation并将您的类的引用作为enableAutoManage方法中的第一个参数传递。或者你可以简单地删除enableAutoManage方法并手动处理connect/disconnect,如下所示。
@Override private synchronized void setUpGClient() { googleApiClient = new GoogleApiClient.Builder(context) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); googleApiClient.connect(); }@Override protected void onStart() { super.onStart(); googleApiClient.connect(); }@Override protected void onStop() { super.onStop(); googleApiClient.disconnect(); }

onLocationChanged方法不会覆盖它超类: - 只需检查要导入的LocationListener。有2个不同的LocationListener。 com.google.android.gms.location.LocationListenerandroid.location.LocationListener。你应该使用com.google.android.gms.location.LocationListener
onRequestPermissionsResult方法不会覆盖它超类: - 你的类DialogChooseYourLocation没有实现ActivityCompat.OnRequestPermissionsResultCallback,所以它没有onRequestPermissionsResult来覆盖。当你调用ActivityCompat.requestPermissions时,是父节点正在接收onRequestPermissionsResult的回调,它不会自动调用你的DialogChooseYourLocation中的任何方法。您需要手动将Activity的onRequestPermissionsResult中的信息传递给DialogChooseYourLocation

    推荐阅读