当前位置: 首页 > 知识库问答 >
问题:

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

白博易
2023-03-14

我在if(text.startswith(prefix))行得到异常。怎么会呢?这到底是怎么回事?

这是我的密码。

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
        }
    }

}
<?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>
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);
    }
}

堆栈跟踪

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

共有1个答案

奚翰海
2023-03-14

在JVM(包括Android)中,超类构造函数体在子类字段初始化器之前运行。因为你能看到线

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

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

由于您对任何CustomEditText都有相同的前缀,因此一个简单的修复方法是将其移到同伴对象中,也许可以使其成为const。否则,您可以使用by lazy

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

所以text也可能仍然为空(您必须检查实现)...

 类似资料: