Kotlin异常:指定为非null的参数为null

时间:2019-11-28 11:53:39

标签: android kotlin

我在第if (text.startsWith(prefix))行遇到了异常。怎么来的?到底是怎么回事?

这是我的代码。

CustomEditText.kt

import android.content.Context
import android.util.AttributeSet
import android.widget.EditText

class CustomEditText : EditText {

    private val prefix: String = "AS"

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun onSelectionChanged(selStart: Int, selEnd: Int) {
        super.onSelectionChanged(selStart, selEnd)
        if (text.startsWith(prefix)) {
            // do something
        }
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.example.myapplication.CustomEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.kt

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Stacktrace

2019-11-28 15:58:46.623 11608-11608/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 11608
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.example.myapplication.CustomEditText
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2821)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2929)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1623)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6678)
        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:830)
     Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.example.myapplication.CustomEditText
     Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.myapplication.CustomEditText
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:12)
        at android.app.Activity.performCreate(Activity.java:7023)
        at android.app.Activity.performCreate(Activity.java:7014)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2774)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2929)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1623)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6678)
        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:830)
     Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter prefix
        at kotlin.text.StringsKt__StringsKt.startsWith(Unknown Source:7)
        at kotlin.text.StringsKt__StringsKt.startsWith$default(Strings.kt:749)
        at com.example.myapplication.CustomEditText.onSelectionChanged(CustomEditText.kt:21)
        at android.widget.TextView.spanChange(TextView.java:9662)
        at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:12121)
        at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:1272)
        at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:773)
        at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:672)
        at android.text.Selection.setSelection(Selection.java:78)
2019-11-28 15:58:46.624 11608-11608/com.example.myapplication E/AndroidRuntime:     at android.text.Selection.setSelection(Selection.java:87)
        at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:312)
        at android.widget.TextView.setText(TextView.java:5532)
        at android.widget.TextView.setText(TextView.java:5400)
        at android.widget.EditText.setText(EditText.java:113)
        at android.widget.TextView.<init>(TextView.java:1652)
        at android.widget.EditText.<init>(EditText.java:88)
        at android.widget.EditText.<init>(EditText.java:84)
        at android.widget.EditText.<init>(EditText.java:80)
        at com.example.myapplication.CustomEditText.<init>(CustomEditText.kt:13)
            ... 26 more

2 个答案:

答案 0 :(得分:2)

在JVM(包括Android)中,超类构造函数主体在子类字段初始化程序之前运行。由于您可以看到

at android.widget.TextView.<init>(TextView.java:1652)

在堆栈跟踪中,问题发生在TextView的构造函数中,并且prefix尚未初始化。

由于任何prefix都具有相同的CustomEditText,一个简单的解决方法是将其移动到伴随对象中,或者使其变为const。否则你可以例如使用by lazy

我们当然在

at android.widget.EditText.setText(EditText.java:113)

也是,所以也许text仍然为空(您必须检查实现)...

答案 1 :(得分:1)

进行此类操作时,请将let函数与safecall(?.)运算符一起使用。 使用Let关键字,该块将在获得非null值时执行。

用法:

{{1}}