java.lang.RuntimeException: Unable to start activity ComponentInfo{....MapActivity}: java.lang.IllegalStateException: Task is not yet complete

Viewed 37

I'm making a simple app with 2 Activities. One is just a Textview, either with a placeholder or user-input coordinates(from map), and a button to move to the next Activity with registerForActivityResult. The next activity is a map with ability to place a mark and press a button to submit the input. When this happens coordinates of said mark should be brought back to the mainActivity and put into textview. However the app crashes when it requests location permission for user, with the following messege:

2022-09-16 00:28:51.357 10442-10442/com.example.ymapstest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ymapstest, PID: 10442
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ymapstest/com.example.ymapstest.MapActivity}: java.lang.IllegalStateException: Task is not yet complete
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        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.IllegalStateException: Task is not yet complete
        at com.google.android.gms.common.internal.Preconditions.checkState(com.google.android.gms:play-services-basement@@18.0.0:2)
        at com.google.android.gms.tasks.zzw.zzf(com.google.android.gms:play-services-tasks@@18.0.1:1)
        at com.google.android.gms.tasks.zzw.getResult(com.google.android.gms:play-services-tasks@@18.0.1:1)
        at com.example.ymapstest.MapActivity.getLastLocation(MapActivity.kt:61)
        at com.example.ymapstest.MapActivity.onCreate(MapActivity.kt:35)
        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) 

Here is a main activity

class MainActivity : AppCompatActivity() {
    private var launcher: ActivityResultLauncher<Intent>? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val textView: TextView = findViewById(R.id.tv_message)
        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            result: ActivityResult ->
            if (result.resultCode == RESULT_OK) {
                val latitude = result.data?.getDoubleExtra("latitude", 0.0).toString()
                val longitude = result.data?.getDoubleExtra("longitude", 0.0).toString()
                textView.text = getString(R.string.address, latitude, longitude)
            }
        }

    }

    fun onClickToMap(view: View) {
        launcher?.launch(Intent(this, MapActivity::class.java))
    }

} 

And a MapActivity

class MapActivity : AppCompatActivity(),  InputListener{

    private lateinit var mapView: MapView
    private val mapAPI = "my api key here"
    private val REQUEST_CODE = 101
    var userInput: Point = Point(0.0, 0.0)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MapKitFactory.setApiKey(mapAPI)
        MapKitFactory.initialize(this)
        setContentView(R.layout.activity_map)
        val userLocation = Point(getLastLocation().latitude, getLastLocation().longitude)

        mapView = findViewById(R.id.mapview)
        mapView.map.move(
            CameraPosition(userLocation, 11.0f, 0.0f, 0.0f),
            Animation(Animation.Type.SMOOTH, 300f), null)

    }

    fun onClick(view: View) {
        if (userInput.equals(Point(0.0, 0.0))) {
            Toast.makeText(this, "You didnt place the point", Toast.LENGTH_LONG).show()
        } else {
            val intent = Intent()
            intent.putExtra("latitude", userInput.latitude)
            intent.putExtra("longitude", userInput.longitude)
            setResult(RESULT_OK, intent)
        }
    }

    private fun getLastLocation(): Location {
        val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
            requestLocationPermission()
            return fusedLocationProviderClient.lastLocation.result
        } else {
            return fusedLocationProviderClient.lastLocation.result
        }
    }

    private fun requestLocationPermission() {
        if (ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION")!= PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), REQUEST_CODE)
        }
    }

    override fun onStop() {
        super.onStop()
        mapView.onStop()
        MapKitFactory.getInstance().onStop()

    }

    override fun onStart() {
        super.onStart()
        mapView.onStart()
        MapKitFactory.getInstance().onStart()
    }

    override fun onMapTap(map: Map, point: Point) {
        mapView.map.mapObjects.clear()
        mapView.map.mapObjects.addPlacemark(point, ImageProvider.fromResource(this, R.drawable.ic_pin))
        userInput = point
    }


}
0 Answers
Related