skip to Main Content

I tried to google my problem but I couldn’t found it, that’s why I ask.

Error: unresolved reference: AlertDialog

Question: How to import AlertDialog because if I google it then I see different methods that causing the error above, e.g. import android.app.AlertDialog or import androidx.appcompat.app.AlertDialog or something else, I need something universal that will never break. I’m wondering why the #include <...> in C++ never get expired and lasts for many years.

Import from https://geeksforgeeks.org/how-to-create-a-custom-yes-no-dialog-in-android-with-kotlin/

import android.app.AlertDialog

Import from https://www.geeksforgeeks.org/how-to-create-an-alert-dialog-box-in-android/

import androidx.appcompat.app.AlertDialog;

Import from https://www.digitalocean.com/community/tutorials/android-alert-dialog-using-kotlin

import android.support.v7.app.AlertDialog;

Code in "MainActivity.kt" (Kotlin with C++ in Android Studio Dolphin | 2021.3.1 Patch 1)

package com.emcengine.emceditor

import android.app.NativeActivity
import android.os.Bundle
import android.content.Context
import android.view.inputmethod.InputMethodManager
import android.view.KeyEvent
import java.util.concurrent.LinkedBlockingQueue

//_/--------------------------------------------------_
import android.app.AlertDialog // error: unresolved reference: AlertDialog
// --------------------------------------------------/

class MainActivity : NativeActivity() {
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    fun showSoftInput() {
        val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.showSoftInput(this.window.decorView, 0)
    }

    fun hideSoftInput() {
        val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(this.window.decorView.windowToken, 0)
    }

    // Queue for the Unicode characters to be polled from native code (via pollUnicodeChar())
    private var unicodeCharacterQueue: LinkedBlockingQueue<Int> = LinkedBlockingQueue()

    // We assume dispatchKeyEvent() of the NativeActivity is actually called for every
    // KeyEvent and not consumed by any View before it reaches here
    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.action == KeyEvent.ACTION_DOWN) {
            unicodeCharacterQueue.offer(event.getUnicodeChar(event.metaState))
        }
        return super.dispatchKeyEvent(event)
    }

    fun pollUnicodeChar(): Int {
        return unicodeCharacterQueue.poll() ?: 0
    }

    //_/--------------------------------------------------_
    fun messageBox() {
        AlertDialog.Builder builder // error: unresolved reference: AlertDialog
        //builder = new AlertDialog.Builder(this)
    }
    // --------------------------------------------------/
}

3

Answers


  1. In general: You don’t have to add the imports by hand. AndroidStudio does all that for you when you just reference another class etc. Try it by removing the import and simply typing AlertDialog somewhere. Then select it from the list of suggestions in the dropdown.

    The one you are probably looking for is import androidx.appcompat.app.AlertDialog.

    Also, in Kotlin the type stands after the variable name (and is optional in that case):

    val builder: AlertDialog = AlertDialog.Builder(context)
    
    Login or Signup to reply.
  2. import android.app.AlertDialog
    

    Old AlertDialog class, shouldn’t be used anymore. Still there for retrocompatibility purposes.

    import android.support.v7.app.AlertDialog
    

    AlertDialog that works on both old and new Android versions while supporting the new API. This requires a new dependency on the support v7 library.

    import androidx.appcompat.app.AlertDialog
    

    Android X (aka new SDK) AlertDialog. This is the one you want to use.

    More on this

    Login or Signup to reply.
  3. This is an important thing to know if you’re doing Android development: there are a whole lot of versions of Android out there, because many devices stop getting updates after only a few years. That means new Android features, and updates and fixes for old features, just don’t exist on a lot of devices.

    That’s a problem when you’re trying to write software that works on all those devices – some stuff just won’t be available. Some stuff will work differently, or display differently. Some stuff will have bugs that were fixed in later versions, and you’ll have to work around those.

    At best, you’ll have to do a lot of if the API version is between this and this checks, with different code paths for different versions. At worst, some things won’t be available at all, and you’ll have to decide between not using those features, locking your app to the range of devices where it is available, or effectively making multiple versions of your app. And then you have to test it all too!


    So what some very nice people at Android produced is the Support Library. This basically backports features and fixes so they’re available on older devices, instead of only being able to use the stuff baked into the version of the OS they have. It also gives you a common interface for those features – you don’t need to use a Compat library for old APIs and the system version for newer ones, you just use the Compat version and that’s it. It’ll work out what to do internally, you don’t need to worry about it.

    They also test all these features, so you can assume they Just Work on every API – you don’t need to do special testing to see if your own workarounds are being handled correctly, someone’s already done that for the Support Library implementations!


    So it’s recommended standard practice to always use the Support Library in your Android projects, and always use its implementation for things over the standard library ones. That’s why your Activities inherit from AppCompatActivity and not Activity, and why the Fragment class that gets automatically imported is the androidx one, and not the standard Fragment (which was actually deprecated several versions ago)

    As for your imports, the trouble is you’re reading tutorials and guides that are years apart, and things change. m0skit0‘s answer explains what each version is – but basically the support library is deprecated, and you want the new AndroidX/Jetpack versions of things.

    AndroidX is a little different, instead of one big support library (which got broken into a handful of still big support libraries by API version) it’s much more granular. That means you have to install the specific libraries you want. AlertDialog (and the Material Design version) are part of the androidx.appcompat:appcompat library (top of the page, or in the package name) – so you need to add that as a dependency, and that’s the import you’ll use in your app.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search