Kotlin-Context.filesDir在模拟器中崩溃

时间:2019-01-17 18:29:00

标签: android kotlin

我是Kotlin的新手,正在开发我的第一个简单应用程序。

我正在尝试创建一个字符串,其中包含将存储我的应用程序文件的路径。

private var fileDir = applicationContext.filesDir

此行导致我的应用崩溃。我正在Android Studio(Pixel XL API 28)中使用仿真器

Logcat显示:

 2019-01-17 13:48:36.024 9704-9704/jcpsnowproductions.mykotlinapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: jcpsnowproductions.mykotlinapp, PID: 9704
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{jcpsnowproductions.mykotlinapp/jcpsnowproductions.mykotlinapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:116)
    at jcpsnowproductions.mykotlinapp.MainActivity.<init>(MainActivity.kt:20)
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
    at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

0 个答案:

没有答案