Issue
I got this error when I tried to debug my app
java.lang.NoSuchMethodError: No virtual method requestPermissions([Ljava/lang/String;I)V in class Lcom/cscodetech/townclap/activity/LoginActivity; or its super classes (declaration of 'com.zestar.myclip.activity.LoginActivity' appears in /data/app/com.zestar.myclip-1/base.apk:classes2.dex)
at com.zestar.myclip.activity.LoginActivity.onCreate(LoginActivity.java:92)
The logcat pointed to this line of code
requestPermissions(new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
What is wrong with that line
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
FirebaseApp.initializeApp(this);
requestPermissions(new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
custPrograssbar = new CustPrograssbar();
sessionManager = new SessionManager(LoginActivity.this);
atCode.setOnFocusChangeListener((view, b) -> {
if (!b) {
// on focus off
String str = atCode.getText().toString();
ListAdapter listAdapter = atCode.getAdapter();
for (int i = 0; i < listAdapter.getCount(); i++) {
String temp = listAdapter.getItem(i).toString();
if (str.compareTo(temp) == 0) {
return;
}
}
atCode.setText("");
}
});
getCodelist();
}
Solution
as requestPermissions
like @TylerV said, try using this way instead
as instructed in
Documentation for how to request app permissions | Android Developers
this should be the right way to request permissions in fragments which goes as this:
- You first initialize this variable in fragment/activity which specifies what should happen if the permissions were granted or not:
private ActivityResultLauncher requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> { {
//result is a map(string permission,boolean granted or not)
//to check if all permissions were granted or not
if(result.containsValue(false)){
// Explain to the user that the feature is unavailable because
// the features requires a permission that the user has denied.
// At the same time, respect the user's decision. Don't link to
// system settings in an effort to convince the user to change
// their decision.
Toast.makeText(getContext(), "Can't continue without the required permissions", Toast.LENGTH_LONG).show();
}
else {
//continue your work flow
}
});
- Then when you need to request for a permission you call launch on the object you declared with an array holding the permission you're requesting :
if (ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.ACCESS_FINE_LOCATION) {
requestPermissionLauncher.launch(new String[]{Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION});
} else // continue your work
Answered By - Omar Shawky
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.