簡體   English   中英

將Java代碼轉換為Kotlin后出現IllegalStateException

[英]IllegalStateException after converting java code to kotlin

java代碼轉換為kotlin后,我得到了IllegalStateException 我用過kotlin extension 在Java中,我的應用程序沒有這種問題,現在使用相同的IllegalStateException進行的每個活動都崩潰了。 我曾嘗試清理,重建並使緩存無效並重新啟動,但是它們都無法解決我的問題。 這是我的活動課,

class SettingsActivity : BaseActivity() {

private val SHOW = "SHOW"
private val CLEAR = "CLEAR"
private var context: Context? = null

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

    context = this

    //at this line I am getting error
    versionInfo.text = BuildConfig.VERSION_NAME

    val cacheStr = "Clear " + clearCache(SHOW) + " MB"
    cache.text = cacheStr

}

private fun clearCache(ACTION: String): Long {
    return null
}
}

這是我的xml文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="General"
        android:paddingStart="16dp"
        android:paddingTop="8dp"
        android:paddingBottom="4dp"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="@android:color/white"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Version"
            android:paddingStart="16dp"
            android:paddingTop="8dp"
            android:textSize="15sp"
            android:textColor="@android:color/white"/>

        <TextView
            android:id="@+id/versionInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1.1.1"
            android:paddingStart="16dp"
            android:paddingBottom="8dp"
            android:textSize="15sp"
            android:textColor="@android:color/white"/>

    </LinearLayout>

</LinearLayout>

這是我的Stacktrace

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.littleapps.wallpaper/com.littleapps.wallpaper.activity.SettingsActivity}: java.lang.IllegalStateException: versionInfo must not be null
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2779)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2857)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6499)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:442)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.IllegalStateException: versionInfo must not be null
    at com.littleapps.wallpaper.activity.SettingsActivity.onCreate(SettingsActivity.kt:30)
    at android.app.Activity.performCreate(Activity.java:7044)
    at android.app.Activity.performCreate(Activity.java:7035)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2732)

這是我的清單文件

<?xml version="1.0" encoding="utf-8"?>

<application
    android:name=".application.MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" />
    <activity
        android:name=".activity.SplashActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_splash"
        android:theme="@style/FullscreenTheme" />

    <activity android:name=".activity.SettingsActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- <__________________FIREBASE MESSAGING SERVICES______________________> -->

    <service
        android:name=".services.MyFirebaseInstanceIdService"
        android:permission="android.permission.BIND_JOB_SERVICE">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".services.MyFirebaseMessagingService"
        android:permission="android.permission.BIND_JOB_SERVICE">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

</application>

確保使用正確的布局和/或TextView。 也許您在不同的布局中具有相同的ID,這會引起問題。

您可以在參考上使用ctrl + left mouse button click組合來檢查這是否是您要更新的適當控件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM