'I don't understand why its returning an empty console log
document.body.append(document.createElement('textarea'));
document.body.append(document.createElement('button'));
document.querySelector('button').addEventListener('click', function () {
const text = document.querySelector('textarea').textContent;
const lowerCase = text.toString().toLowerCase();
console.log(lowerCase);
});
I am sorry if I don't display stuff properly here, I'm new to this place, and fairly new to coding but trying to figure out stuff.
Solution 1:[1]
That Java code is defining a member variable named diceAnimation (well, wifiAnimation in the video) of type AnimationDrawable with an initial value of null (that is implicit), and then inside onCreate() it is assigning dealerDiceView1.background() to that variable. Since the type of a view's background is only reported as being a Drawable, they have to cast the returned value to the more specific AnimationDrawable type to be able to assign it to that variable. Java casts items by preceding the item with the type in parentheses.
Kotlin does not have member variables. It has properties instead. Since this diceAnimation never needs to be null and we set its value in onCreate() it is appropriate to make it a lateinit var property so we don't have to give it an inititial value. So we can give it a type of AnimationDrawable instead of AnimationDrawable?. You want to avoid using nullable types whenever you can because they are harder to work with and more error-prone.
You also have to cast the returned value when assigning it, for the reason I mentioned in the first paragraph. Casting is done with the as keyword in Kotlin.
If you are unfamiliar with what casting is, it is basically telling the compiler that you promise that some object is of a more specific type than the compiler knows, so the compiler can start treating it like that more specific type. In this case, since a View's background can be any type of Drawable, at the time you get the background, the compiler only knows that it's a Drawable. By casting it to AnimationDrawable you promise the compiler that you know that the background actually is specifically an AnimationDrawable, and then the compiler will allow you to assign it to variables or properties of that more specific type.
class MainActivity : AppCompatActivity() {
private lateinit var diceAnimation: AnimationDrawable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var dealerDiceView1 = findViewById<ImageView>(R.id.dealerDice1)
dealerDiceView1.setBackgroundResource(R.drawable.dice_animation)
diceAnimation = dealerDiceView1.background() as AnimationDrawable
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
