Can't instantiate MapboxNavigation object - android

I'm just starting with the Mapbox implementation in a native Android app and I just can't get it done.
The most basic thing (instantiating a MapboxNavigation can not be done). I get this error and the app closes:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testemapbox, PID: 29380
java.lang.NoSuchMethodError: No static method getInstance()Lcom/mapbox/common/LogConfiguration; in class Lcom/mapbox/common/LogConfiguration; or its super classes (declaration of 'com.mapbox.common.LogConfiguration' appears in /data/app/~~CoIOdGRtIbaTlwz2rI-MFg==/com.example.testemapbox-_1CkcSTRk6UI5QXFz-w_6g==/base.apk)
at com.mapbox.navigation.utils.internal.LoggerProvider.initialize(LoggerProvider.kt:23)
at com.mapbox.navigation.core.MapboxNavigation.<init>(MapboxNavigation.kt:396)
at com.mapbox.navigation.core.MapboxNavigation.<init>(MapboxNavigation.kt:223)
at com.mapbox.navigation.core.MapboxNavigationProvider.create(MapboxNavigationProvider.kt:23)
at com.example.testemapbox.MainActivity.onCreate(MainActivity.kt:20)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
And here is the snippet of my code:
package com.example.testemapbox
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.mapbox.navigation.base.options.NavigationOptions
import com.mapbox.navigation.base.trip.model.RouteProgress
import com.mapbox.navigation.core.MapboxNavigationProvider
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
class MainActivity : AppCompatActivity(), RouteProgressObserver {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navigationOptions = NavigationOptions.Builder(this)
.accessToken(getString(R.string.mapbox_access_token))
.build()
val mapboxNavigation = MapboxNavigationProvider.create(navigationOptions)
}
}
I need to know if I'm doing something wrong. I know the code isn't useful (just don't do anything) but I suppose that I should have to be able to do something like that.

Related

Error message when testing an app in the Android Studio emulator: 'The app keeps stopping'

When I try an app I was developing in the Android Studio emulator, the following message appears: 'App keeps stopping' and it doesn't open.
This is the code for the MainActivity:
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.bookapplication.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.libroLeidos.setOnClickListener { Toast.makeText(this, "Entrando en Libros Leídos", Toast.LENGTH_LONG).show()
val primerIntent = Intent(this, librosLeidos::class.java)
startActivity(primerIntent)
}
binding.libroPendiente.setOnClickListener { Toast.makeText(this, "Entrando en Libros Pendientes", Toast.LENGTH_LONG).show()
val segundoIntent = Intent(this, librosPendientes::class.java)
startActivity(segundoIntent)
}
}}
On the other hand, in the LogCat, I get this message:
Process: com.example.bookapplication, PID: 14919
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bookapplication/com.example.bookapplication.MainActivity}: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
at android.app.Activity.requireViewById(Activity.java:3231)
at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:368)
at androidx.navigation.Navigation.findNavController(Navigation.java:58)
at androidx.navigation.ActivityKt.findNavController(Activity.kt:30)
at com.example.bookapplication.MainActivity.onCreate(MainActivity.kt:24)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
The problem is that I don't know where to go to fix it. I am new to this and there are many things that are beyond me. I have found other similar questions but I have not understood well how to proceed.
Thank you very much.
The stacktrace from logcat points you to the call to findNavController (which appears to be on line 24 in your source code), where it says that it can't find the id R.id.nav_host_fragment_content_main in your view.
Is R.id.nav_host_fragment_content_main defined in activity_main.xml? If so, the second call to setContentView() is replacing that view with whatever is in binding.root. In that case, removing the second call could fix your issue.
If R.id.nav_host_fragment_content_main is supposed to be defined in binding.root, I think you'll need to include the implementation of com.example.bookapplication.databinding.ActivityMainBinding in your question so we can better understand what' going on.
In any case, you shouldn't call setContentView() twice, as #TylerV said.

getSystemService NullPointerException

package com.example.myapplication
import android.app.Activity
import android.content.Context
import android.os.*
import android.util.Log
import android.view.DragEvent
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintSet
import androidx.wear.widget.SwipeDismissFrameLayout
class MetroMapActivity : Activity()
{
private lateinit var image:ImageView
private var gesture:ScaleGestureDetector? = null
private var scaleFactor = 1.0f
private lateinit var swipeview:SwipeDismissFrameLayout
private lateinit var toast: Toast
private lateinit var buttonone:Button
private lateinit var vibratorManager: VibratorManager
private lateinit var vibrator: Vibrator
#RequiresApi(Build.VERSION_CODES.S)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_metro_map)
swipeview = findViewById(R.id.swipe)
toast = Toast.makeText(this,"a",Toast.LENGTH_SHORT)
image = findViewById(R.id.metromap)
buttonone = findViewById<Button>(R.id.button)
gesture = ScaleGestureDetector(this, ScaleListener())
swipeview.setOnTouchListener(swipetouch())
buttonone.setOnTouchListener(buttontouch())
vibratorManager = this.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
//vibrator = vibratorManager.defaultVibrator
//vibrator.vibrate(VibrationEffect.createOneShot(100, 30))
//image.setOnTouchListener(imagetouch())
}
i'm making wearOS applincation.
when i call getSystemService() i get NullPointerException. it seems getSystemService() keep returns null but i don't know why.
i also wrote <uses-permission android:name="android.permission.VIBRATE"/ > in AndroidManifest.xml.
i can't even get clue. please help...please....
i once wrote getSystemService() at OnResume() because someone told me that it
something is not initialized yet(?) but it also was not working.
this is stacktrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 27345
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type android.os.VibratorManager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3456)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3612)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2073)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:7690)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
Caused by: java.lang.NullPointerException: null cannot be cast to non-null type android.os.VibratorManager
at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:31)
at android.app.Activity.performCreate(Activity.java:8009)
at android.app.Activity.performCreate(Activity.java:7993)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3612) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2073) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:246) 
at android.app.ActivityThread.main(ActivityThread.java:7690) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) 
I/Process: Sending signal. PID: 27345 SIG: 9
VIBRATOR_MANAGER_SERVICE was added in API level 31 (Android 12) and devices running on lower API levels know nothing about it.
On older API levels you can use VIBRATOR_SERVICE instead to retrieve a Vibrator (rather than a VibratorManager).

Android Studio Fatal Exception: main counter app

Hey guys I wanted to build a counter app. But this error pops up when I want to test it:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.change, PID: 7804
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.change/com.example.change.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property fighter1 has not been initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property fighter1 has not been initialized
at com.example.change.MainActivity.onCreate(MainActivity.kt:21)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
I don't know if I have coded everything correctly because I just started learning Android Studio, so I still don't know a lot about it. This is my code (I'm new to Kotlin):
package com.example.change
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private var counter1 = 0;
private lateinit var counterRed1: TextView
private lateinit var fighter1: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
counterRed1 = findViewById(R.id.counterRed1);
fighter1.setOnClickListener {
counter1 ++
counterRed1.text = (Integer.toString(counter1))
}
}
}
You have missed initializing your fighter1 button field. You need to initialize it just like you've initialized the counterRed1 text field.
counterRed1 = findViewById(R.id.counterRed1);
fighter1 = findViewById(R.id.fighter1);
Also make sure your activity_main layout file has a Button with the id fighter1.

Instantation Error when running Android studio App with OkHTTTP Using Kotlin

using OkHTTP I am trying to retrieve a text file and put hat into the textView on the main activity,
Gradle will say build successful but the emulator app will just crash on launch and provide this error.
I have added the correct permissions in the AndroidManifest.xml and the correct implementation in the gradle build
package com.example.vectortestapi
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import okhttp3.*
import java.io.IOException
abstract class MainActivity : AppCompatActivity() {
private val mTextView: TextView = findViewById<TextView>(R.id.resultText);
private val client = OkHttpClient()
private val url = "https://publicobject.com/helloworld.txt"
private val request: Request = Request.Builder()
.url(url)
.build();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getAPI();
}
private fun getAPI(){
client.newCall(request).enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
val inputStream = response.body?.string()
runOnUiThread{
mTextView.text = inputStream
}
}
})
}
}
produces this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vectortestapi, PID: 9900
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.vectortestapi/com.example.vectortestapi.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.vectortestapi.MainActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.vectortestapi.MainActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
I/Process: Sending signal. PID: 9900 SIG: 9
Abstract classes cannot be instantiated, but they can be subclassed.

callback of registerForActivityResult(RequestPermission() does not run

I'm trying to request permistions as it is written in the https://developer.android.com/training/permissions/requesting
Something is wrong.
In Logcat there is no Log.d output from callback of registerForActivityResult.
proggy() not run from callback. It set permissions okey.
But
when I changed
implementation 'androidx.activity:activity-ktx:1.2.0-alpha06'
to
implementation 'androidx.activity:activity-ktx:1.2.0-alpha07'
I got:
2020-07-30 01:42:55.698 12259-12259/tk.kvakva.myapplication D/AndroidRuntime: Shutting down VM
2020-07-30 01:42:55.701 12259-12259/tk.kvakva.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: tk.kvakva.myapplication, PID: 12259
java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.kvakva.myapplication/tk.kvakva.myapplication.MainActivity}: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at androidx.fragment.app.FragmentActivity.checkForValidRequestCode(FragmentActivity.java:715)
at androidx.fragment.app.FragmentActivity.validateRequestPermissionsRequestCode(FragmentActivity.java:730)
at androidx.core.app.ActivityCompat.requestPermissions(ActivityCompat.java:500)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:178)
at androidx.activity.result.ActivityResultRegistry$3.launch(ActivityResultRegistry.java:147)
at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:42)
at tk.kvakva.myapplication.MainActivity.onCreate(MainActivity.kt:39)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
class MainActivity : AppCompatActivity() {
val TAG = "MY_MainAct"
fun proggy(){
Log.d(TAG, "!!!!!!!!!!!!!!!!!! proggy() !!!!!!!!!!!!!!!!")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val permReqLuncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){
Log.d(TAG,"!!!!!!!++++++++++++++!!!!!CALL BACK!!!!!!!!!!+++++++++++++++!!!!!!!!!!!!!!!!!!!!!")
if(it) {
Log.d(TAG,"registerForActivityResult(ActivityResultContracts.RequestPermission()){ it: $it")
proggy();
}
else
Log.d(TAG,"Perm Not Granted")
}
when(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
PackageManager.PERMISSION_GRANTED -> {
Log.d(TAG,"in onCreate when checkSelfPerm grated")
proggy()
}
PackageManager.PERMISSION_DENIED -> {
Log.d(TAG,"!!!!!!!!!!! in onCreate when checkSelfPermistion DENIED")
permReqLuncher.launch( Manifest.permission.WRITE_EXTERNAL_STORAGE )
}
}
}
}
I added
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha07'
Callback runs.
Exceptions gone.

Categories

Resources