I'm trying to take permission to change imageView with another image from library that users choose. So, I connected RecipesFragment with MainActivity with NavHostFragment.But when I try to run the app it's giving crash and closing up.You can see the code , crashes from logcat down there and the problems.(The onclick Id of imageView2 is chooseImage from xml)
class RecipesFragment : Fragment() {
private lateinit var binding: FragmentRecipesBinding
var choosenPicture : Uri? = null
var choosenBitmap : Bitmap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentRecipesBinding.inflate(inflater, container, false)
// Inflate the layout for this fragment
return binding.root
}
fun chooseImage(view: View) {
activity.let {
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
//There is no permission already so we need to take permission
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1)
//If it doesnt work use READ_EXTERNAL_STORAGE
}else{
//There is already permission so, there is no need to take permission again
val galleryIntent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent,2)
}
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if(requestCode == 1){
if(grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//We have permission now
val galleryIntent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent,2)
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == 2 && resultCode == Activity.RESULT_OK && data != null){
choosenPicture = data.data
}
try{
context?.let {
if(choosenPicture != null){
if(Build.VERSION.SDK_INT >= 28){
val source = ImageDecoder.createSource(it.contentResolver,choosenPicture!!)
choosenBitmap = ImageDecoder.decodeBitmap(source)
binding.imageView2.setImageBitmap(choosenBitmap)
}else{
choosenBitmap = MediaStore.Images.Media.getBitmap(it.contentResolver,choosenPicture)
binding.imageView2.setImageBitmap(choosenBitmap)
}
}
}
}catch (e: Exception){
e.printStackTrace()
}
super.onActivityResult(requestCode, resultCode, data)
}
E FATAL EXCEPTION: main Process: com.alperenavan.recipes, PID: 9811 java.lang.IllegalStateException: Could not find method chooseImage(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatImageView with id 'imageView2' at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:506) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:464) at android.view.View.performClick(View.java:7506) at android.view.View.performClickInternal(View.java:7483) at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0) at android.view.View$PerformClick.run(View.java:29334) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)`
I tried to make an imageView which can be changed from users photo gallery with permission but when I try to take permission and run the app it crashed.
