CameraX PreviewView to Bitmap instead of a File - android

I know there are other questions that was answered to many times and I tried to find the answer, but I couldn't understand of it most. I want to make the camera to take a photo, but instead of making a physical image file, I want it directly to make it into a bitmap and display it in an ImageView from a different activity, however I constantly getting the Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference, that's because of the viewFinder.bitmap is a null and decided to make a seperate function getBitmap(): Bitmap?. I don't know what it wants me to do honestly or any lead from this, all I want to do is to make the photo straight to ImageView as a bitmap. Thank you in advance.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.camerax5, PID: 5472
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camerax5/com.example.camerax5.VirtualPreview}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3141)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3284)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1972)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7179)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:677)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:842)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:633)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
at com.example.camerax5.MainActivity.getBitmap(MainActivity.kt:118)
at com.example.camerax5.VirtualPreview.onCreate(VirtualPreview.kt:19)
at android.app.Activity.performCreate(Activity.java:7335)
at android.app.Activity.performCreate(Activity.java:7326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
So the problem was occured on this line:
viewFinder = findViewById(R.id.previewView)
Here is the full code of what I was trying to do:
MainActivity.kt and underneath the MainActivity code is the other class with another activity to show the bitmap in the ImageView, I added this way is because I don't want to separate them and making them confusing:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewFinder = findViewById(R.id.previewView)
//Permissions
if (allPermissions()) {
startCamera()
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
//Capture button
val captureBtn: Button = findViewById(R.id.captureButton)
captureBtn.setOnClickListener {
takePhoto()
}
outputDirectory = outputDirectoryFolder()
cameraExecutor = Executors.newSingleThreadExecutor()
}
private fun takePhoto() {
Toast.makeText(baseContext, "Processing ..", Toast.LENGTH_SHORT).show()
//Directly to open the new Activity
val intent = Intent(this, VirtualPreview::class.java)
startActivity(intent)
}
fun getBitmap(): Bitmap? {
//Preview to Bitmap
viewFinder = findViewById(R.id.previewView)
val source = viewFinder.bitmap
val newWidth = 200
val newHeight = 200
val bitmapWidth = source?.width
val bitmapHeight = source?.height
//Matrix
val matrix = Matrix()
val scaleWidth = newWidth.toFloat() / bitmapWidth!!
val scaleHeight = newHeight.toFloat() / bitmapHeight!!
matrix.postScale(scaleWidth, scaleHeight)
return Bitmap.createBitmap(source, 0, 0, newWidth, newHeight, matrix, true)
}
}
class VirtualPreview : AppCompatActivity() {
private lateinit var acceptButton: Button
private lateinit var denyButton: Button
private lateinit var imageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_virtual_preview)
val bitmapPreview = MainActivity().getBitmap()
val drawableBitmap = BitmapDrawable(resources, bitmapPreview)
imageView = findViewById(R.id.imagePreview)
imageView.setImageDrawable(drawableBitmap)
acceptButton = findViewById(R.id.acceptButton)
acceptButton.setOnClickListener {
}
denyButton = findViewById(R.id.denyButton)
denyButton.setOnClickListener {
//delete the matrix and returns to the main activity
finish()
}
}
}

val bitmapPreview = MainActivity().getBitmap() - This way you are creating a new instance of MainActivity and the new instance do not have any thing in preview view, also this method is not recommended. Better store bitmap inside MainActivity and then pass that bitmap to VirtualPreview activity in constructor (in bundle it won't work). It won't be a problem for your case, as you're scaling the bitmap to 200x200. and for large bitmap, store in file, and use that file. And for your case, you can create another imageview in mainactivity and show the bitmap in that imageview instead of creating another activity.

Related

How do I change the image?

I am trying to change the image when I click the button, but it doesn't work. Can anyone tell me how to fix this? I think there are something to do with the declaration
val afterEatingImage:ImageView="R.drawable.after_cookie"
and I tried = (R.drawable.after_cookie), R.drawable.after_cookie, after_cookie
Maybe I should not use Val for an image but I don't know what to use. So you can see this is a basic question and I am a beginner so pls help.
fun eating(view: View?) {
val afterEatingText:String="I am so full!!"
displayMessage(afterEatingText)
val afterEatingImage:ImageView="R.drawable.after_cookie"
displayImage(afterEatingImage)
}
private fun displayMessage(message: String) {
val afterEatingText = findViewById<View>(R.id.before_eating) as TextView
afterEatingText.text = message
}
private fun displayImage(image: ImageView){
val afterEatingImage: findViewById<View>(R.id.before_cookie) as ImageView
afterEatingImage.setImageResource(image)
}
}
The id of my ImageView is android:id="#+id/before_cookie"
The names of 2 imagefiles are before_cookie and after_cookie.
Red errors are as follows.
Another way to do this:
fun eatCookie(view: View?) {
// Find a reference to the ImageView in the layout. Change the image.
val imageView = findViewById<View>(R.id.before_eating) as ImageView
imageView.setImageResource(R.drawable.after_cookie)
// Find a reference to the TextView in the layout. Change the text.
val textView = findViewById<View>(R.id.before_eating) as TextView
textView.text = "I'm so full"
}
The crash result after I click the button.
Message from Logcat
05-17 12:22:11.216 3029-3029/com.example.afterclicking E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.afterclicking, PID: 3029
java.lang.IllegalStateException: Could not find method eating(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)
at android.view.View.performClick(View.java:4780)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
If I use val textView = findViewById<View>(R.id.before_eating)
then red alert appears.
If I use val textView = findViewById<View>(R.id.before_eating) as TextView then it will be fine, Why is that?
What you are doing is assigning the String R.drawable.after_cookie to your ImageView afterEatingImage. You pass this ImageView to your method and try to assign it as the image resource.
What you should do is the following
Pass the integer R.drawable.after_cookie to your method
Assign this parameter to your ImageView
This will become something like this
fun eating(view: View?) {
val afterEatingText:String="I am so full!!"
displayMessage(afterEatingText)
displayImage(R.drawable.after_cookie) //Change this
}
private fun displayMessage(message: String) {
val afterEatingText = findViewById<View>(R.id.before_eating) as TextView
afterEatingText.text = message
}
private fun displayImage(imageResource: Int) { //Change this
val afterEatingImage: ImageView = findViewById<ImageView>(R.id.before_cookie)
afterEatingImage.setImageResource(imageResource)
}
Note: I am assuming this is all done in your activity. In case you are doing this in your fragment, you need a reference of your view to find your ImageView

Error of java.lang.RuntimeException: Unable to instantiate activity ComponentInfo KOTLIN/AndroidStudio

I am trying to do a copy of Space Invaders with Kotlin and Android Studio to learn more about this kind of development but I have this error happening :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spaceinvaders, PID: 3982
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spaceinvaders/com.example.spaceinvaders.GameView}: java.lang.InstantiationException: java.lang.Class<com.example.spaceinvaders.GameView> has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.spaceinvaders.GameView> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1216)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2902)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:201) 
at android.app.ActivityThread.main(ActivityThread.java:6864) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
I have two activities: one for the menu and one for the game. When I try to pass from one menu to the other by clicking on a button, it crashes.
Here there are the other codes :
HomeScreen :
class HomeScreen : Activity(), View.OnClickListener{
lateinit var nbAliens : TextView
lateinit var SPACE_SHIP : Bitmap //L'image choisi par le joueur
var ALIENS_NUMBER : Int = 0 //Le nombre d'ennemi choisi par le joueur
var flagOK : Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ecran_accueil)
nbAliens = findViewById(R.id.textViewNbEnnemis)
btnMinus.setOnClickListener(this)
btnPlus.setOnClickListener(this)
imgBtnSpaceShip1.setOnClickListener(this)
imgBtnSpaceShip2.setOnClickListener(this)
btnStart.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v){
btnMinus ->{
//Modify the textView et change ALIEN_NUMBER
var i = nbAliens.text.toString().toInt()
if (i > 7) i--
nbAliens.text = i.toString()
ALIENS_NUMBER = i
}
btnPlus ->{
var i = nbAliens.text.toString().toInt()
if (i < 21) i++
nbAliens.text = i.toString()
ALIENS_NUMBER = i
}
imgBtnSpaceShip1 ->{
/*When you click in the imageButton, you chose your SpaceShip Image
The first one's background goes green while the other becomes all grey
*/
val color = Color.argb(130,255,255,255)
imgBtnSpaceShip1.setColorFilter(Color.TRANSPARENT)
imgBtnSpaceShip2.setBackgroundColor(color)
imgBtnSpaceShip2.setColorFilter(color)
imgBtnSpaceShip1.setBackgroundColor(Color.GREEN)
SPACE_SHIP = BitmapFactory.decodeResource(resources, R.drawable.space_ship1)
flagOK = true
}
imgBtnSpaceShip2 ->{
val color = Color.argb(130,255,255,255)
imgBtnSpaceShip2.setColorFilter(Color.TRANSPARENT)
imgBtnSpaceShip1.setBackgroundColor(color)
imgBtnSpaceShip1.setColorFilter(color)
imgBtnSpaceShip2.setBackgroundColor(Color.GREEN)
SPACE_SHIP = BitmapFactory.decodeResource(resources, R.drawable.space_ship2)
flagOK = true
}
btnStart -> {
//Change from HomeScreen to GameView
if (flagOK) {
val intent: Intent =
Intent(applicationContext, GameView::class.java)
startActivity(intent)
finish()
}else {
Toast.makeText(this, "You have to pick a spaceship to start!", Toast.LENGTH_LONG).show()
}
}
}
}
}
GameView :
class GameView #JvmOverloads constructor (context: Context, var imgSpaceShip: Bitmap, attributes: AttributeSet? = null, defStyleAttr: Int = 0): View(context, attributes,defStyleAttr){
private lateinit var imgAlienGreen : Bitmap
private lateinit var imgAlienRed : Bitmap
private lateinit var imgAlienWhite : Bitmap
private var alien_margin by Delegates.notNull<Float>()
private var alien_side by Delegates.notNull<Float>()
val game : Game = Game()
val backgroundPaint = Paint()
//Load images and resize it
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
alien_margin = width*0.025f
alien_side = (width - (game.NB_ALIENS_COLUMNS+1)*alien_margin)/game.NB_ALIENS_COLUMNS
//There is another class Game for the logic of the game
try{
imgAlienGreen = BitmapFactory.decodeResource(resources, R.drawable.alien_green)
imgAlienGreen = Bitmap.createScaledBitmap(imgAlienGreen, alien_side.toInt(), alien_side.toInt(), true)
imgAlienRed = BitmapFactory.decodeResource(resources, R.drawable.alien_red)
imgAlienRed = Bitmap.createScaledBitmap(imgAlienRed, alien_side.toInt(), alien_side.toInt(), true)
imgAlienWhite = BitmapFactory.decodeResource(resources, R.drawable.alien_white)
imgAlienWhite = Bitmap.createScaledBitmap(imgAlienWhite, alien_side.toInt(), alien_side.toInt(), true)
imgSpaceShip = Bitmap.createScaledBitmap(imgSpaceShip, alien_side.toInt(), alien_side.toInt(), true)
}catch (exception : Exception) {
Log.e("ERROR", "Cannot load images");
}
}
//Build the rectangles where the images will be set on.
private fun alienRectF(index: Int) : RectF{
val x : Float = alien_margin + alien_side
val y : Float = height + 0.17f
return RectF(x, y, x+alien_side, y+alien_side)
}
//It finishes here but I will continue coding after resolving it
}
I have no idea of what is going on. Thanks for the help!
To use startActivity function, you should navigate from Activity to Activity, but GameView is not activity, it is a View (extends from View, but should be Activity)

Kotlin creating Bitmap must not be null

lateinit var bitmap: Bitmap
onCreate{
bitmap = BitmapFactory.decodeResource(applicationContext.resources, Color.TRANSPARENT)
}
I am trying to create a transparent bitmap like above. However, it gives me this error.
Caused by: java.lang.IllegalStateException: BitmapFactory.decodeReso…urces, Color.TRANSPARENT) must not be null
What did I wrong?
As mentioned by #commonWare, either declare bitmap nullable:
var bitmap: Bitmap?
or handle null when you call decodeResource:
bitmap = BitmapFactory.decodeResource(applicationContext.resources, Color.TRANSPARENT) ?: DEFAULT_BITMAP
where DEFAULT_BITMAP is a default Bitmap you have created earlier.
And better if you have control over decodeResource method, make the return type non nullable and return some default value instead of null :
fun decodeResource(...): Bitmap {
.....
return bitmap ?: DEFAULT_BITMAP // return a default value instead of returning null
}

Canvas is Blank After Making Draw() Calls

Following up with my problem I describe here i'm trying to draw some shapes to a canvas that isn't associated with any view/layout (it's totally in memory & never drawn to the screen) and then use the bitmap I drew in my activity.
The problem:
It seems like my draw method never gets called. I have some log messages within it and I never see them print, and when I view the bitmap during runtime in Android Studio's debugger its always blank.
I've read various posts about having to call setWillNotDraw(false) to get the onDraw() method in a custom view to trigger, but because i'm never going to render this canvas to the screen my custom class extends Drawable() instead of View(context which doesn't include that method. This seems like a good choice since View includes lots of logic for user touches and other actions that my no-UI, background-generated drawable won't use.
That being said I still only saw blank bitmaps and no log messages from onDraw() when I extended View instead of Drawable and called setWillNowDraw(false) in the classes' constructor.
What's causing my canvas to always be blank?
class CustomImage : Drawable() {
var bitmap: Bitmap
private var barcodeText = Paint(ANTI_ALIAS_FLAG).apply {
color = Color.BLACK
textSize = 24f
}
private var circlePainter = Paint(ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
}
init {
bitmap = Bitmap.createBitmap(LABEL_SIDE_LENGTH, LABEL_SIDE_LENGTH, Bitmap.Config.ARGB_8888)
}
override fun draw(canvas: Canvas) {
Timber.d("canvas: In draw")
canvas.setBitmap(bitmap)
canvas.apply {
drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
drawText("002098613", LABEL_SIDE_LENGTH - 50f, 50f, barcodeText)
drawCircle(200f, 200f, 100f, circlePainter)
}
Timber.d("canvas: $canvas")
}
override fun setAlpha(alpha: Int) {
}
override fun getOpacity(): Int {
return PixelFormat.OPAQUE
}
override fun setColorFilter(colorFilter: ColorFilter?) {
}
companion object {
const val LABEL_SIDE_LENGTH = 1160
}
}
class MyActivity: AppCompatActivity(){
private lateinit var customImgBitmap: Bitmap
override fun onCreate(savedInstanceState: Bundle?) {
customImgBitmap = createImgBitmap()
val test = POLabelGenerator
}
...
private fun createImgBitmap(): Bitmap {
return CustomImage.bitmap
}
}

Android Studio Convert Java To Kotlin 2 errors

After Convert Java File to Kotlin File, gives me two errors in same line: In code below show variables and function with error
lateinit var ncArr: Array<ImageButton>
lateinit var xBitmap: Bitmap
lateinit var oBitmap: Bitmap
lateinit var intArr: IntArray
lateinit var btnStartGame: Button
lateinit var btnMenu: Button
var stop: Boolean = false
var gameMode: Int = 0
var umove: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onStart()
setContentView(R.layout.activity_game)
ncArr = arrayOfNulls(9)
ncArr[0] = findViewById(R.id.nc0) as ImageButton
ncArr[1] = findViewById(R.id.nc1) as ImageButton
ncArr[2] = findViewById(R.id.nc2) as ImageButton
ncArr[3] = findViewById(R.id.nc3) as ImageButton
ncArr[4] = findViewById(R.id.nc4) as ImageButton
ncArr[5] = findViewById(R.id.nc5) as ImageButton
ncArr[6] = findViewById(R.id.nc6) as ImageButton
ncArr[7] = findViewById(R.id.nc7) as ImageButton
ncArr[8] = findViewById(R.id.nc8) as ImageButton
ncArr[0].setOnClickListener(this)
ncArr[1].setOnClickListener(this)
ncArr[2].setOnClickListener(this)
ncArr[3].setOnClickListener(this)
ncArr[4].setOnClickListener(this)
ncArr[5].setOnClickListener(this)
ncArr[6].setOnClickListener(this)
ncArr[7].setOnClickListener(this)
ncArr[8].setOnClickListener(this)
xBitmap = BitmapFactory.decodeResource(resources, R.drawable.x)
oBitmap = BitmapFactory.decodeResource(resources, R.drawable.o)
intArr = IntArray(9)
for (i in 0..8) {
intArr[i] = 0
}
btnMenu = findViewById(R.id.btnMenu) as Button
btnStartGame = findViewById(R.id.btnStartGame) as Button
btnMenu.setOnClickListener(this)
btnStartGame.setOnClickListener(this)
stop = false
gameMode = intent.getIntExtra("game_mode", 1)
umove = 1
}
in line ncArr = arrayOfNulls(9)
There are errors:
I trying to fix this problem but i don't have more ideas...
Any sollution?
thanks in advance
ncArr = arrayOfNulls(9)
The type of ncArr will be Array<ImageButton?>
ncArr[0] = findViewById(R.id.nc0) as ImageButton
You are using unsafe cast operator as, because the cast operator throws an exception if the cast is not possible. Thus, we call it unsafe. What happens if
findViewById(R.id.nc0) as ImageButton
return null, then null cannot assign to ImageButton, it will throw an exception and make your app crash.
To avoid the above error you can use
ncArr[0] = findViewById(R.id.nc0) as ImageButton?
or using safe cast operator as?
ncArr[0] = findViewById(R.id.nc0) as? ImageButton
Remember apply for ncArr[0] to ncArr[8].
Update: Based on your request, you can use this solution:
lateinit var ncArr: Array<ImageButton>
lateinit var xBitmap: Bitmap
lateinit var oBitmap: Bitmap
lateinit var intArr: IntArray
lateinit var btnStartGame: Button
lateinit var btnMenu: Button
var stop: Boolean = false
var gameMode: Int = 0
var umove: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onStart()
setContentView(R.layout.activity_game)
ncArr = arrayOf(
findViewById<ImageButton>(R.id.nc0).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc1).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc2).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc3).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc4).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc5).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc6).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc7).apply{setOnClickListener(this#GameActivity)},
findViewById<ImageButton>(R.id.nc8).apply{setOnClickListener(this#GameActivity)}
)
xBitmap = BitmapFactory.decodeResource(resources, R.drawable.x)
oBitmap = BitmapFactory.decodeResource(resources, R.drawable.o)
// Your code here
...
}
You trying to put Nullable array into NonNull. You may declare intArr as Array<ImageButton?> and everything will work.

Categories

Resources