我有时在我的应用程序中发现java.lang.IllegalStateException:
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)
我调查了一下后,这个问题是由 Intent.getStringExtra()引起的,其代码如下:
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")
}
已经在许多测试设备中进行了测试,除非我查看了Google故障报告,否则无法重现此崩溃问题。 IllegalStateException到底是什么,并且由于意图返回值而发生这种情况?
答案 0 :(得分:2)
您可以使用Elvis运算符ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +
geom_line(position="jitter", color="red", size=2) + theme_bw() +
theme(text=element_text(size = 16, family = "IndieFlower"))
处理可能的空值
?:
或仅在值不为null时设置该值,例如与val intent : Intent = this.intent
mArticleImg = intent.getStringExtra("articleImg") ?: "null-value-fallback"
mArticle = intent.getParcelableExtra("data") ?: "null-value-fallback"
或if
:
.let{..}
答案 1 :(得分:0)
使用这些检查以确保密钥可用
if (intent.hasExtra("articleImg")) {
mArticleImg = intent.getStringExtra( "articleImg" )
}
if (intent.hasExtra("data")) {
mArticle = intent.getParcelableExtra("data")
}
答案 2 :(得分: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