Annotations are not allowed here when moving to a separated class
I try to separate my Location finding code from the Main activity, so I moved the entire related code to a different class:
(*I began with Android today and Java is also fairly new to me, so sorry if this is a silly question)
public class LocationCode {
private LocationCallback mLocationCallback;
private TextView tv;
private Button button;
private FusedLocationProviderClient mFusedLocationClient;
private final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 5;
public LocationCode (Context context, TextView textView, Button btn) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
this.tv = textView;
this.button = btn;
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
Double latDouble = location.getLatitude();
String latString = latDouble.toString();
Double longtDouble = location.getLongitude();
String longtString = longtDouble.toString();
tv.setText(latString + " " + longtString);
}
}
};
@Override
public void onRequestPermissionsResult ( int requestCode,
String permissions, int grantResults){
TextView tv1 = this.tv;
Button button = this.button;
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//
} else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
null /* Looper */);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
tv1.setText("Not Granted!");
button.setVisibility(View.VISIBLE);
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
}
}
From MainActivity I should probably call this code like so:
LocationCode locationCode = new LocationCode(this, textView, Button);
But I get the following error on the second @Override
(The one above public void onRequestPermissionsResult
). Why is that, and how can I fix it? Or there is a better way to separate this code to make it look more neat in the MainActivity?
android
|
show 1 more comment
I try to separate my Location finding code from the Main activity, so I moved the entire related code to a different class:
(*I began with Android today and Java is also fairly new to me, so sorry if this is a silly question)
public class LocationCode {
private LocationCallback mLocationCallback;
private TextView tv;
private Button button;
private FusedLocationProviderClient mFusedLocationClient;
private final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 5;
public LocationCode (Context context, TextView textView, Button btn) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
this.tv = textView;
this.button = btn;
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
Double latDouble = location.getLatitude();
String latString = latDouble.toString();
Double longtDouble = location.getLongitude();
String longtString = longtDouble.toString();
tv.setText(latString + " " + longtString);
}
}
};
@Override
public void onRequestPermissionsResult ( int requestCode,
String permissions, int grantResults){
TextView tv1 = this.tv;
Button button = this.button;
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//
} else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
null /* Looper */);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
tv1.setText("Not Granted!");
button.setVisibility(View.VISIBLE);
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
}
}
From MainActivity I should probably call this code like so:
LocationCode locationCode = new LocationCode(this, textView, Button);
But I get the following error on the second @Override
(The one above public void onRequestPermissionsResult
). Why is that, and how can I fix it? Or there is a better way to separate this code to make it look more neat in the MainActivity?
android
1
To override some methods the current class must be a child of a super class with that class, so in this caseonRequestPermissionsResult
may not be a method from super class of LocationCode. And its obvious since it does not extend any class.
– Xenolion
Nov 18 '18 at 20:21
Also, you have that annotatedonRequestPermissionsResult()
method inside the constructor. It can't go there, which is what the current error message is telling you.
– Mike M.
Nov 18 '18 at 20:24
onRequestPermissionsResult
comes fromFragmentActivity
class. In yourLocationCode
you don't extend from FragmentActivity therefore you don't have that method. Also you are missing a parenthesis to close your constructor
– Mustafa Berkay Mutlu
Nov 18 '18 at 20:25
Could you please help me then make my class valid? I tried moving stuff, extending the class but I still get all sorts of errors. An example of working class would be very helpful, thank you guys
– GIDroid
Nov 18 '18 at 20:42
For now, keep the permissions stuff in theActivity
. That is, moveonRequestPermissionsResult()
back to theActivity
. It's more work than you'll likely want to do, at this point, to separate that from theActivity
. (It's also rather clunky, as far as design, and I wouldn't recommend it.) That will take care of the current error.
– Mike M.
Nov 18 '18 at 20:51
|
show 1 more comment
I try to separate my Location finding code from the Main activity, so I moved the entire related code to a different class:
(*I began with Android today and Java is also fairly new to me, so sorry if this is a silly question)
public class LocationCode {
private LocationCallback mLocationCallback;
private TextView tv;
private Button button;
private FusedLocationProviderClient mFusedLocationClient;
private final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 5;
public LocationCode (Context context, TextView textView, Button btn) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
this.tv = textView;
this.button = btn;
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
Double latDouble = location.getLatitude();
String latString = latDouble.toString();
Double longtDouble = location.getLongitude();
String longtString = longtDouble.toString();
tv.setText(latString + " " + longtString);
}
}
};
@Override
public void onRequestPermissionsResult ( int requestCode,
String permissions, int grantResults){
TextView tv1 = this.tv;
Button button = this.button;
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//
} else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
null /* Looper */);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
tv1.setText("Not Granted!");
button.setVisibility(View.VISIBLE);
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
}
}
From MainActivity I should probably call this code like so:
LocationCode locationCode = new LocationCode(this, textView, Button);
But I get the following error on the second @Override
(The one above public void onRequestPermissionsResult
). Why is that, and how can I fix it? Or there is a better way to separate this code to make it look more neat in the MainActivity?
android
I try to separate my Location finding code from the Main activity, so I moved the entire related code to a different class:
(*I began with Android today and Java is also fairly new to me, so sorry if this is a silly question)
public class LocationCode {
private LocationCallback mLocationCallback;
private TextView tv;
private Button button;
private FusedLocationProviderClient mFusedLocationClient;
private final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 5;
public LocationCode (Context context, TextView textView, Button btn) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
this.tv = textView;
this.button = btn;
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
Double latDouble = location.getLatitude();
String latString = latDouble.toString();
Double longtDouble = location.getLongitude();
String longtString = longtDouble.toString();
tv.setText(latString + " " + longtString);
}
}
};
@Override
public void onRequestPermissionsResult ( int requestCode,
String permissions, int grantResults){
TextView tv1 = this.tv;
Button button = this.button;
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//
} else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
null /* Looper */);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
tv1.setText("Not Granted!");
button.setVisibility(View.VISIBLE);
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
}
}
From MainActivity I should probably call this code like so:
LocationCode locationCode = new LocationCode(this, textView, Button);
But I get the following error on the second @Override
(The one above public void onRequestPermissionsResult
). Why is that, and how can I fix it? Or there is a better way to separate this code to make it look more neat in the MainActivity?
android
android
asked Nov 18 '18 at 20:17
GIDroidGIDroid
34
34
1
To override some methods the current class must be a child of a super class with that class, so in this caseonRequestPermissionsResult
may not be a method from super class of LocationCode. And its obvious since it does not extend any class.
– Xenolion
Nov 18 '18 at 20:21
Also, you have that annotatedonRequestPermissionsResult()
method inside the constructor. It can't go there, which is what the current error message is telling you.
– Mike M.
Nov 18 '18 at 20:24
onRequestPermissionsResult
comes fromFragmentActivity
class. In yourLocationCode
you don't extend from FragmentActivity therefore you don't have that method. Also you are missing a parenthesis to close your constructor
– Mustafa Berkay Mutlu
Nov 18 '18 at 20:25
Could you please help me then make my class valid? I tried moving stuff, extending the class but I still get all sorts of errors. An example of working class would be very helpful, thank you guys
– GIDroid
Nov 18 '18 at 20:42
For now, keep the permissions stuff in theActivity
. That is, moveonRequestPermissionsResult()
back to theActivity
. It's more work than you'll likely want to do, at this point, to separate that from theActivity
. (It's also rather clunky, as far as design, and I wouldn't recommend it.) That will take care of the current error.
– Mike M.
Nov 18 '18 at 20:51
|
show 1 more comment
1
To override some methods the current class must be a child of a super class with that class, so in this caseonRequestPermissionsResult
may not be a method from super class of LocationCode. And its obvious since it does not extend any class.
– Xenolion
Nov 18 '18 at 20:21
Also, you have that annotatedonRequestPermissionsResult()
method inside the constructor. It can't go there, which is what the current error message is telling you.
– Mike M.
Nov 18 '18 at 20:24
onRequestPermissionsResult
comes fromFragmentActivity
class. In yourLocationCode
you don't extend from FragmentActivity therefore you don't have that method. Also you are missing a parenthesis to close your constructor
– Mustafa Berkay Mutlu
Nov 18 '18 at 20:25
Could you please help me then make my class valid? I tried moving stuff, extending the class but I still get all sorts of errors. An example of working class would be very helpful, thank you guys
– GIDroid
Nov 18 '18 at 20:42
For now, keep the permissions stuff in theActivity
. That is, moveonRequestPermissionsResult()
back to theActivity
. It's more work than you'll likely want to do, at this point, to separate that from theActivity
. (It's also rather clunky, as far as design, and I wouldn't recommend it.) That will take care of the current error.
– Mike M.
Nov 18 '18 at 20:51
1
1
To override some methods the current class must be a child of a super class with that class, so in this case
onRequestPermissionsResult
may not be a method from super class of LocationCode. And its obvious since it does not extend any class.– Xenolion
Nov 18 '18 at 20:21
To override some methods the current class must be a child of a super class with that class, so in this case
onRequestPermissionsResult
may not be a method from super class of LocationCode. And its obvious since it does not extend any class.– Xenolion
Nov 18 '18 at 20:21
Also, you have that annotated
onRequestPermissionsResult()
method inside the constructor. It can't go there, which is what the current error message is telling you.– Mike M.
Nov 18 '18 at 20:24
Also, you have that annotated
onRequestPermissionsResult()
method inside the constructor. It can't go there, which is what the current error message is telling you.– Mike M.
Nov 18 '18 at 20:24
onRequestPermissionsResult
comes from FragmentActivity
class. In your LocationCode
you don't extend from FragmentActivity therefore you don't have that method. Also you are missing a parenthesis to close your constructor– Mustafa Berkay Mutlu
Nov 18 '18 at 20:25
onRequestPermissionsResult
comes from FragmentActivity
class. In your LocationCode
you don't extend from FragmentActivity therefore you don't have that method. Also you are missing a parenthesis to close your constructor– Mustafa Berkay Mutlu
Nov 18 '18 at 20:25
Could you please help me then make my class valid? I tried moving stuff, extending the class but I still get all sorts of errors. An example of working class would be very helpful, thank you guys
– GIDroid
Nov 18 '18 at 20:42
Could you please help me then make my class valid? I tried moving stuff, extending the class but I still get all sorts of errors. An example of working class would be very helpful, thank you guys
– GIDroid
Nov 18 '18 at 20:42
For now, keep the permissions stuff in the
Activity
. That is, move onRequestPermissionsResult()
back to the Activity
. It's more work than you'll likely want to do, at this point, to separate that from the Activity
. (It's also rather clunky, as far as design, and I wouldn't recommend it.) That will take care of the current error.– Mike M.
Nov 18 '18 at 20:51
For now, keep the permissions stuff in the
Activity
. That is, move onRequestPermissionsResult()
back to the Activity
. It's more work than you'll likely want to do, at this point, to separate that from the Activity
. (It's also rather clunky, as far as design, and I wouldn't recommend it.) That will take care of the current error.– Mike M.
Nov 18 '18 at 20:51
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365037%2fannotations-are-not-allowed-here-when-moving-to-a-separated-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365037%2fannotations-are-not-allowed-here-when-moving-to-a-separated-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
To override some methods the current class must be a child of a super class with that class, so in this case
onRequestPermissionsResult
may not be a method from super class of LocationCode. And its obvious since it does not extend any class.– Xenolion
Nov 18 '18 at 20:21
Also, you have that annotated
onRequestPermissionsResult()
method inside the constructor. It can't go there, which is what the current error message is telling you.– Mike M.
Nov 18 '18 at 20:24
onRequestPermissionsResult
comes fromFragmentActivity
class. In yourLocationCode
you don't extend from FragmentActivity therefore you don't have that method. Also you are missing a parenthesis to close your constructor– Mustafa Berkay Mutlu
Nov 18 '18 at 20:25
Could you please help me then make my class valid? I tried moving stuff, extending the class but I still get all sorts of errors. An example of working class would be very helpful, thank you guys
– GIDroid
Nov 18 '18 at 20:42
For now, keep the permissions stuff in the
Activity
. That is, moveonRequestPermissionsResult()
back to theActivity
. It's more work than you'll likely want to do, at this point, to separate that from theActivity
. (It's also rather clunky, as far as design, and I wouldn't recommend it.) That will take care of the current error.– Mike M.
Nov 18 '18 at 20:51