1

Please I am working with kotlin and when I tried to move from one Activity to another using an intent, i got an error on the java keyword which says "unresolved reference" when i click to see some suggestions they told me to create an Extension property of Kclass.java. I don't really know what is the issue as i have tried so many suggestions but still cant solve it. Here is the code.

class WelcomeActivity : AppCompatActivity() {

    var firebaseUser: FirebaseUser? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
    }

    override fun onStart() {
        super.onStart()
        firebaseUser = FirebaseAuth.getInstance().currentUser
        if(firebaseUser !=null){
            val intent = Intent(this@WelcomeActivity,MainActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
}
Ukeme Elijah
  • 157
  • 3
  • 13
  • 1
    Hi @Ukeme please edit the question and copy paste the logcat/stack trace output which is showing the error. – Tonnie Mar 10 '22 at 10:41
  • 1
    Don't Understand Your Question Can you describe it in brief? – Jayesh Dankhara Mar 10 '22 at 10:51
  • The error is not in the logcat. the error is showing in this "java" keyword "val intent = Intent(this@WelcomeActivity,MainActivity::class.java)" which says unresolved reference and one of the suggestions is to create an extension property of the Kclass .java – Ukeme Elijah Mar 10 '22 at 11:00
  • what i meant is this .java in the "val intent = Intent(this@WelcomeActivity,MainActivity::class.java)" is underlined red with a message that says "unresolved reference" @JayeshDankhara – Ukeme Elijah Mar 10 '22 at 11:02
  • Try to clean and rebuild project @Ukeme – Jinal Patel Mar 10 '22 at 11:48

1 Answers1

1

You might probably want to move these codes from onStart() to onCreate()

firebaseUser = FirebaseAuth.getInstance().currentUser
    if(firebaseUser !=null){
        val intent = Intent(this@WelcomeActivity,MainActivity::class.java)
        startActivity(intent)
        finish()
    }

This is so that the target Activity (MainActivity) starts immediately the WelcomeActivity has been fully created (not just when it starts) and when the Firebase user's authentication has been verified.

Also, the error might be coming from this snippet.

intent = Intent(this@WelcomeActivity,MainActivity::class.java)"

Which you say "is underlined red with a message that says "unresolved reference".

What you need to start the MainActivity is a Context, I think that is where the problem is.

So try any of the following.

intent = Intent(this, MainActivity::class.java)

intent = Intent(getApplicationContext(), MainActivity::class.java)

If you do not understand what Context is all about then check this StackOverflow post here.

Hope this was helpful.

IODevBlue
  • 71
  • 4
  • I have tried it and still not working, I don't know if it's because i updated my kotlin plugin recently that is giving me all these issues because toLowerCase function that i wanted to use is not working – Ukeme Elijah Mar 10 '22 at 13:33