Fragmente müssen vor ihrer Erstellung das Register registerForActivityResult() aufrufenAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 Fragmente müssen vor ihrer Erstellung das Register registerForActivityResult() aufrufen

Post by Anonymous »

Ich arbeite an einer Shopping-App und habe ein Fragment zum Hinzufügen von Produkten zur Produktliste erstellt. Ich benötige eine Leseberechtigung für den externen Speicher, um ein Foto zum Produkt hinzuzufügen. Deshalb versuche ich, registerForActivityResult() in meinem Fragment zu verwenden, und erhalte diese Fehlermeldung. Ich bin neu in der Android-Programmierung und weiß daher nicht, ob ich sie richtig verwende.
Ich habe jede Antwort auf eine andere Frage ausprobiert, aber auch sie hat nicht funktioniert. Entschuldigung für mein schlechtes Englisch. Vielen Dank für Ihre Hilfe.

Code: Select all

java.lang.IllegalStateException: Fragment AddActivity after being created. Fragments must call rHgS5orgPvsuzJ7shn8vw28y1XaePY4uDP() before they are created (i.e.  initialization, onAttach(), or onCreate()).
at androidx.fragment.app.Fragment.prepareCallInternal(Fragment.java:3482)
at androidx.fragment.app.Fragment.rHgS5orgPvsuzJ7shn8vw28y1XaePY4uDP(Fragment.java:3449)
at com.example.TheDentalSupplies.AddActivity.showImageChooser(AddActivity.kt:120)
at com.example.TheDentalSupplies.AddActivity.requestPermission(AddActivity.kt:96)
at com.example.TheDentalSupplies.AddActivity.onViewCreated$lambda-1(AddActivity.kt:66)
at com.example.TheDentalSupplies.AddActivity.lambda$gx8TAUa5Jk3WDkvGOWG0oB39kiM(Unknown Source:0)
at com.example.TheDentalSupplies.-$$Lambda$AddActivity$gx8TAUa5Jk3WDkvGOWG0oB39kiM.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)

AddActivity.kt:

Code: Select all

class AddActivity : Fragment() {
private lateinit var img:ImageView
private lateinit var requestLauncher: ActivityResultLauncher

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

requestLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) {
if(it){
showImageChooser(img)
}else{
Toast.makeText(requireContext(), "PERM", Toast.LENGTH_SHORT).show()
}
}
return inflater.inflate(R.layout.addpost, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val image1 = view.findViewById(R.id.addImage1)
val image2 = view.findViewById(R.id.addImage2)
val image3 = view.findViewById(R.id.addImage3)
val image4 = view.findViewById(R.id.addImage4)
val image5 = view.findViewById(R.id.addImage5)

image1.setOnClickListener {
requestPermission(image1)
}
image2.setOnClickListener {
requestPermission(image2)
}
image3.setOnClickListener {
requestPermission(image3)
}
image4.setOnClickListener {
requestPermission(image4)
}
image5.setOnClickListener {
requestPermission(image5)
}
val publishBtn = view.findViewById(R.id.addPublishBtn)

publishBtn.setOnClickListener { addItem(view) }

}

private fun requestPermission(imageView: ImageView) {
img = imageView

if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

showImageChooser(imageView)
}
else{

ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), Constants.READ_STORAGE_PERMISSION_CODE)
requestLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)

}

}

fun showImageChooser(imageView: ImageView) {
val galleryIntent = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)

val launchSomeActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
if (data != null){
try {
val mSelectedImageFileUri = data.data!!

//setUserPic.setImageURI(Uri.parse(selectedImageFileUri.toString()))
GlideLoader(requireContext()).loadUserPicture(mSelectedImageFileUri,imageView)

//FireStoreClass().uploadImgCloud(requireActivity(), mSelectedImageFileUri)

}catch (e:  IOException){
e.printStackTrace()
Toast.makeText(
requireContext(),
"Image selection failed",
Toast.LENGTH_SHORT
).show()
}
}
}
}

launchSomeActivity.launch(galleryIntent)
}

private fun addItem(view: View) {
//val image1 = view.findViewById(R.id.addImage1)
val addProductName = view.findViewById(R.id.addTitleEditText).text.toString()
val addProductDesc = view.findViewById(R.id.addDescEditText).text.toString()
val addProductPrice = view.findViewById(R.id.addPrice).text.toString()
val addProductCat = view.findViewById(R.id.selectCategoryEt).text.toString()

when{
TextUtils.isEmpty(addProductName.trim { it  Toast.makeText(activity,
"Please Enter Product Name.",
Toast.LENGTH_SHORT).show()

TextUtils.isEmpty(addProductDesc.trim { it  Toast.makeText(activity,
"Please Enter Product Description.",
Toast.LENGTH_SHORT).show()

TextUtils.isEmpty(addProductCat.trim { it  Toast.makeText(activity,
"Please Select Product Category.",
Toast.LENGTH_SHORT).show()

TextUtils.isEmpty(addProductPrice.trim { it  Toast.makeText(activity,
"Please Enter Product Price.",
Toast.LENGTH_SHORT).show()

else -> {
//val index = 0
val firebaseUser: String = Firebase.auth.currentUser!!.uid
val product = ProductItem(R.drawable.add_box,
addProductName,
0.0,
"Serhat Yilmaz",
addProductPrice.toInt(),
addProductDesc,
addProductCat.toInt(),
firebaseUser)

//Datasource.productList.add(index,product)
FireStoreClass().addProduct(requireActivity(), product)
//Toast.makeText(activity, "Product Published!", Toast.LENGTH_SHORT).show()
val action = AddActivityDirections.actionAddPostNavActivityToProductFragment()
view.findNavController().navigate(action)
}
}
}

}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post