when I am trying to import these two lines in my code
"import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;"
showing error like cannot resolve symbol fragment.
Did you declare the dependencies in the Gradle?
It´s the first thing you need to do before an import.
Try this:
dependencies {
// Java language implementation
implementation "androidx.fragment:fragment:$fragment_version"
// Kotlin
implementation "androidx.fragment:fragment-ktx:$fragment_version"
}
And check out this link! Fragments Info
Related
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)
}
// \--------------------------------------------------/
}
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)
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
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.
trying to use passport-custom and the very first line, from pseudocode at npmjs, errors out:
import passportCustom from 'passport-custom';
The is no default import in index.js when I open it up under node_modules/passport-custom/lib
I must be missing something fundamental here, don't know what though
Try to use CommonJS const passportCustom = require("passport-custom") You probably have older version of Node.js which does not support ES6 modules.
There is no default export. So you will have to name the items you want to import (put them in curly braces).
//Example:
import { a,b,c,d} from 'youPackage';
//Your case:
import { passportCustom } from 'passport-custom';
Above are called named imports. When a package exports one item by default using: export default passportCustom ;, you could have use your code. You can access the code of the package to have a look for yourself.
Error Shoing For this line
Toolbar toolbar = findViewById(R.id.toolbar);
Error:
Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at com.example.mcq.CategoriesActivity.onCreate(CategoriesActivity.java:25)
To use androidx, in your build.gradle file, add the following:
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
}
Now you can use androidx toolbar as follows:
androidx.appcompat.widget.Toolbar
Else if you have not refactored to androidx, then just use:
android.support.v7.widget.Toolbar
Hope you will find the solution.
You are using a wrong library for the toolbar, check your import statement and replace
import androidx.appcompat.widget.Toolbar;
With:
import android.widget.Toolbar;
In your activity
I am trying to use org.xhtmlrenderer:core-renderer:R8pre2 in a groovy script, but I get a Linkage error:
Caught: java.lang.LinkageError: loader constraint violation in interface
itable initialization: when resolving method
"org.apache.xerces.dom.NodeImpl.getOwnerDocument()Lorg/w3c/dom/Document;"
the class loader (instance of org/codehaus/groovy/tools/RootLoader) of the
current class, org/apache/xerces/dom/NodeImpl, and the class loader (instance of
<bootloader>) for interface org/w3c/dom/Node have different Class objects for
the type getOwnerDocument used in the signature
I've already googled a lot and found a lot of answer like these:
Dealing with "Xerces hell" in Java/Maven?
XercesImpl in conflict with JavaSE 6's internal xerces implementation. Both are needed... what can be done?
So, one solution could be to use javas endorsed mechanism to resolve the conflict, but I would like to make my script independent from such a "workaround". The script should run out of the box.
Next thing I was giving a try was to exclude the right dependency like this
#Grapes([
#Grab('org.xhtmlrenderer:core-renderer:R8pre2'),
#GrabExclude('xml-apis:xml-apis')
])
but didn't succeed...
Any ideas?
PS: here is the script which creates the error:
#Grapes([
#Grab('org.xhtmlrenderer:core-renderer:R8pre2'),
])
import org.w3c.dom.Document
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
def dbf = DocumentBuilderFactory.newInstance()
DocumentBuilder builder = dbf.newDocumentBuilder()
Document doc = builder.parse(new ByteArrayInputStream("<html></html>".getBytes()))
Thanx to #dmahapatro, I checked my configuration and found that I dropped some jars in {usrhome}/.groovy a long time ago. Removed these and now everything work like a charm...
I'm trying to make the nested presenter in GWT.
I've been "copy & paste" code from the sample GWTP project #http://code.google.com/p/gwt-platform/wiki/SimpleNestedSample
At one point the code
#ContentSlot
public static final Type<RevealContentHandler<?>> contentSlot = new Type<RevealContentHandler<?>>();
gave me error and i am not able to find which package to import by eclipse suggestion to resolve error by Type.
(Been trying all the possible "Type" packages by eclipse suggestion such as
import com.google.gwt.i18n.server.Type;
import com.google.gwt.dev.asm.Type;
import com.sun.tools.javac.code.Type;
)
Can anyone tell what package to import?
import com.google.gwt.event.shared.GwtEvent.Type;