-2

The program stop when it reach the findViewById and I can't figure out why :

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.splash_screen)

    handler.postDelayed({ setContentView(R.layout.activity_main) }, 500) // 5 seconds
    val imageView: ImageView = findViewById(R.id.splashScreenIMG)
    Glide.with(this).load(R.drawable.sligames_waiting).into(imageView)
}

    fun showButtons()
{
    val view: View = findViewById(R.id.boutonOui)
    val cx = view.width / 2
    val cy = view.height / 2
    val finalRadius = hypot(cx.toDouble(), cy.toDouble()).toFloat()
    val anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0f, finalRadius)
    // make the view visible and start the animation
    view.visibility = View.VISIBLE
    anim.duration = 1500
    anim.start()
}

I execute this function from another file with this line :

MainActivity().showButtons()

Here's the stack trace :

2020-06-01 12:55:02.783 22557-22557/com.example.yourmajesty E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.yourmajesty, PID: 22557
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:163)
    at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
    at android.content.Context.obtainStyledAttributes(Context.java:738)
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
    at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
    at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
    at com.example.yourmajesty.MainActivity.showButtons(MainActivity.kt:122)
    at com.example.yourmajesty.Dialog.dialogChanger(Dialog.kt:26)
    at com.example.yourmajesty.MainActivity$dialogBoxDiseappear$2.run(MainActivity.kt:190)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7695)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Thanks in advance

2 Answers2

2
MainActivity().showButtons()

You can't initiate an object from your activity. You can only launch it and bundle data with its Intent

Please refere to this thread If you're trying to communicate with MainActvitiy from a fragment

Mohamed Nageh
  • 1,963
  • 1
  • 19
  • 27
0

For creating a reference to a class's objects in Kotlin, you can use

(activity as MainActivity!!)?.showButtons()

What this will do is it will create a reference of already running class instead of creating one more instance which will always give NPE for Views as it's not instantiated.

Also, remember referencing views of other Class or Fragment only works when that class has a running instance, you can reference functions which don't actually need to create views and only need the Context, but referencing Views or any objects which need to be initialized first give the NPE.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50