我在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
在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
也可能仍然为空(您必须检查实现)...
我有java代码,我改成kotlin了,我的代码是用pdf-viewer库显示pdf的,我不明白为什么我的代码是错误的,下面是错误: 指定为non-null的是null参数:方法kotlin.jvm.internal.intrinsics.CheckParameterIsNotNull,inputStream参数 这是我的密码
不确定是什么原因导致了这种情况,但我正在尝试从api请求数据,该api包含一个对象数组。如果我将结果打印到控制台,则数据是正确的,除了之外,我希望是一个对象数组。我不明白我错过了什么? 我得到了这个错误: 谁能给我指一下正确的方向吗?课程代码如下: 示例JSON:
我得到这个错误 为了那条线 以下是整个代码。这段代码最初是在java中,我使用Android Studio将其转换为静态编程语言,但现在我得到了这个错误。我试图重建和清理这个项目,但没有成功。
在将适配器代码转换为Kotlin后,我遇到了此错误: 充气行时会触发错误: 显然,一些不应该为null的参数是null,kotlin检查它。问题是我甚至不能调试新的kotlin代码。