将Java代码转换为Kotlin后出现IllegalStateException

时间:2018-08-06 17:25:09

标签: android xml kotlin illegalstateexception

将我的IllegalStateException代码转换为java后,我得到了kotlin。我使用了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>

1 个答案:

答案 0 :(得分:0)

确保使用正确的布局和/或TextView。也许您在不同的布局中具有相同的ID,这会引起问题。

您可以参考使用ctrl + left mouse button click组合来检查这是否是您要更新的适当控件。