I'm trying to write a simplification of the AlertDialog, so I can be more at home with kotlin, if I cannot make my own language do Android then make kotlin act like PureBasic would is the theory.
The problem I've ran into is this error:
4027-4027/com.example.cardgamexxx E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cardgamexxx, PID: 4027
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:1444)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:469)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
at android.app.Dialog.show(Dialog.java:505)
at android.app.AlertDialog$Builder.show(AlertDialog.java:1157)
at com.example.cardgamexxx.Requester.messageRequester(Requester.kt:33)
at com.example.cardgamexxx.MainActivity.onCreate$lambda-3(MainActivity.kt:695)
at com.example.cardgamexxx.MainActivity.lambda$Zt8ODWx7LLRfq4535q7hQg5xQfw(Unknown Source:0)
at com.example.cardgamexxx.-$$Lambda$MainActivity$Zt8ODWx7LLRfq4535q7hQg5xQfw.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
With this code:
package com.example.cardgamexxx
import android.app.AlertDialog
import android.content.Context
import com.example.cardgamexxx.RequesterType.MessageRequester_Ok
import com.example.cardgamexxx.RequesterType.MessageRequester_YesNo
import com.example.cardgamexxx.RequesterType.MessageRequester_YesNoCancel
//------------------------------------------------------------------------------
// Very PureBasic'ish 'Requester' dialogs.
//------------------------------------------------------------------------------
object RequesterType {
const val MessageRequester_Ok = 1 // to have the 'ok' only button (default)
const val MessageRequester_YesNo = 2 // to have 'yes' or 'no' buttons
const val MessageRequester_YesNoCancel = 3 // to have 'yes', 'no' and 'cancel' buttons
}
class Requester {
fun inputRequester() {
}
fun messageRequester(appContext: Context,title: String,message: String,flags: Int) : Int {
var result: Int = 0
if( flags == MessageRequester_Ok ) {
AlertDialog.Builder(appContext)
.setTitle(title) // R.string.question_title
.setMessage(message) // R.string.question_message
.setPositiveButton("Ok") { _, _ -> result = 0 }
.show()
}
if( flags == MessageRequester_YesNo ) {
AlertDialog.Builder(appContext)
.setTitle(title) // R.string.question_title
.setMessage(message) // R.string.question_message
.setPositiveButton("Yes") { _, _ -> result = 0 }
.setNegativeButton("No") { _, _ -> result = 1 }
.show()
}
if( flags == MessageRequester_YesNoCancel ) {
AlertDialog.Builder(appContext)
.setTitle(title) // R.string.question_title
.setMessage(message) // R.string.question_message
.setPositiveButton("Yes") { _, _ -> result = 0 }
.setNegativeButton("No") { _, _ -> result = 1 }
.setNegativeButton("Cancel") { _, _ -> result = 2 }
.show()
}
return result
}
fun openFileRequester() {
}
fun pathRequester() {
}
fun saveFileRequester() {
}
}
Another problem is the requesters while open are supposed to be 'code' blocking meaning, not that this matters all 'that' much, it would however be nice if "messageRequester()" returned a result which the code calling "messageRequester()" could then change flow according to the answer given.
Am I on the wrong path? do I need to just dump this idea and re-create the wheel and make my own from the ground up.