我自横刀向天笑,去留肝胆两昆仑。这篇文章主要讲述无法在Android中的Dialog类中添加一些覆盖方法相关的知识,希望能为你提供帮助。
我创建了一个扩展Dialog类的类。在本课程中,我想在此课程中添加“位置更改”代码。当我尝试做同样的事情时,它向我展示了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.LocationListener
和android.location.LocationListener
。你应该使用com.google.android.gms.location.LocationListener
onRequestPermissionsResult
方法不会覆盖它超类: - 你的类DialogChooseYourLocation
没有实现ActivityCompat.OnRequestPermissionsResultCallback,所以它没有onRequestPermissionsResult
来覆盖。当你调用ActivityCompat.requestPermissions
时,是父节点正在接收onRequestPermissionsResult
的回调,它不会自动调用你的DialogChooseYourLocation
中的任何方法。您需要手动将Activity的onRequestPermissionsResult中的信息传递给DialogChooseYourLocation
。推荐阅读
- 如何在Android中的主/细分片段之间进行适当的导航()
- 如何在使用Android Jetpack导航时禁用导航图标
- WrapPanel ListBox没有换行
- 使用Google NLP API将实体字符串传递给主要活动(Android)
- 在AndroidTestCase中使用@Ignore
- 在Android Studio中将默认注释设置为@NonNull
- Android @Inject和@InjectView注释含义
- Android @NonNull的用处
- Android React Native卡在闪屏上