Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
IllegalStateException Caused By Intent?

I sometimes found java.lang.IllegalStateException in my application:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2825)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2886)
  at android.app.ActivityThread.-wrap12 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1623)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:186)
  at android.app.ActivityThread.main (ActivityThread.java:6509)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:914)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:804)

Caused by: java.lang.IllegalStateException: 
  at com.****.onCreate (***.kt:35) <-- here is the Intent.getStringExtra(..)
  at android.app.Activity.performCreate (Activity.java:6992)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1119)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2778)

After i looked into it, this issue is caused by Intent.getStringExtra() with code as follows:

private lateinit var mArticle : ArticleModel
private lateinit var mArticleImg : String

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

    val intent : Intent = this.intent;
    mArticleImg = intent.getStringExtra( "articleImg" ) <-- here is where it crashed
    mArticle = intent.getParcelableExtra("data")
}

Have tested this in many testing devices and could not reproduce this crash issue except when i looked into google crash report. What exactly is IllegalStateException and does this happen because of the intent return value?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You may handle possible null value with elvis operator ?:

val intent : Intent = this.intent
mArticleImg = intent.getStringExtra("articleImg") ?: "null-value-fallback"
mArticle = intent.getParcelableExtra("data") ?: "null-value-fallback"

or set the value only if it is not null, e.g. with if or .let{..}:

val intent : Intent = this.intent
intent.getStringExtra("articleImg")?.let { mArticleImg = it }
intent.getParcelableExtra("data")?.let { mArticle = it }
over 4 years ago · Santiago Trujillo Relatório

0

Use these checks to ensure that key is available

if (intent.hasExtra("articleImg")) {    
    mArticleImg = intent.getStringExtra( "articleImg" ) 
}

if (intent.hasExtra("data")) {
    mArticle = intent.getParcelableExtra("data")
}
over 4 years ago · Santiago Trujillo Relatório

0

Step 1: In the Button Click and Putting String Through Intent... as below

ll_Admin_Milk.setOnClickListener(View.OnClickListener {
            val intent = Intent(this, EggAndMilkActivity::class.java)
            intent.putExtra("Eggs","Eggs")
            intent.putExtra("Milk","Milk")
            startActivity(intent)
        })

Step 2: In the EggAndMilkActivity.Class i am Getting the intent value as below....

       val intent : Intent = this.intent
        intent.getStringExtra("Eggs")?.let { Eggs = it }
        intent.getStringExtra("Milk")?.let { Milk= it }

Step 3: Do not Forget to add this line of Code **?.let { Eggs = it }** other wise you will get illegalArgumentException..


-Happy Coding @Ambilpura
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda