iT邦幫忙

0

我寫的APP一直閃退...

我是用android studio開發跟jave撰寫
最近開始自學android app開發
想請問各位大大底下的錯誤是什麼意思

2021-10-14 14:17:59.264 9241-9241/com.example.viewmodelshp16 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.viewmodelshp16, PID: 9241
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewmodelshp16/com.example.viewmodelshp16.MainActivity}: java.lang.RuntimeException: An exception happened in constructor of class com.example.viewmodelshp16.MyViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.RuntimeException: An exception happened in constructor of class com.example.viewmodelshp16.MyViewModel
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:132)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.example.viewmodelshp16.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.lifecycle.SavedStateHandle.contains(java.lang.String)' on a null object reference
at com.example.viewmodelshp16.MyViewModel.(MyViewModel.java:21)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:120)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
at com.example.viewmodelshp16.MainActivity.onCreate(MainActivity.java:23) 
at android.app.Activity.performCreate(Activity.java:7009) 
at android.app.Activity.performCreate(Activity.java:7000) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

這是viewmodel的程式碼

https://ithelp.ithome.com.tw/upload/images/20211014/20143063tZ6nLEvAt9.jpg
https://ithelp.ithome.com.tw/upload/images/20211014/20143063jiM9ZwjlQM.jpg

這是Activity的程式碼
https://ithelp.ithome.com.tw/upload/images/20211014/201430636YFXchsbDU.jpg

Hankz iT邦新手 3 級 ‧ 2021-10-14 14:38:35 檢舉
可以考慮把程式碼貼上來
版上的大大比較有辦法幫你
目前只能知道在下面這了類出了RuntimeException而已:
com.example.viewmodelshp16.MyViewModel

1 個回答

6
Hankz
iT邦新手 3 級 ‧ 2021-10-14 15:00:24
最佳解答

這個是不是拼錯了/images/emoticon/emoticon01.gif
https://ithelp.ithome.com.tw/upload/images/20211014/20139878ljiHJcgfuN.jpg

fan4444 iT邦新手 5 級 ‧ 2021-10-14 15:28:29 檢舉

真的欸...
我還花兩天看viewmodel哪裡有問題
感謝大大幫忙! 感激不盡!

Hankz iT邦新手 3 級 ‧ 2021-10-14 16:24:03 檢舉

能解決問題就好

我要發表回答

立即登入回答