我正在尝试在Android中自定义SwitchMaterial
类,如下所示
class TipSwitchMaterial @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = android.R.attr.switchStyle
) : SwitchMaterial(context, attrs, defStyleAttr) {
}
我在活动xml中使用TipSwitchMaterial
,如下所示:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
tools:context=".SidActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<com.omnieyes.omnilite.view.TipSwitchMaterial
android:id="@+id/event_record_sort_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/sort"
android:tooltipText="@string/tooltip_sort"
app:layout_constraintBottom_toBottomOf="@+id/predict_button"
app:layout_constraintEnd_toStartOf="@id/predict_button"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/predict_button" />
...
然而,NPE
将在进入该活动后立即发生:
2023-03-23 15:21:36.630 17706-17706/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.omnieyes.omnilite, PID: 17706
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at android.text.StaticLayout.<init>(StaticLayout.java:455)
at androidx.appcompat.widget.SwitchCompat.makeLayout(SwitchCompat.java:982)
at androidx.appcompat.widget.SwitchCompat.onMeasure(SwitchCompat.java:905)
at android.view.View.measure(View.java:25466)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:811)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:466)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:134)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:278)
at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:120)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1594)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1708)
at android.view.View.measure(View.java:25466)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1412)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.widget.ScrollView.onMeasure(ScrollView.java:452)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:747)
at android.view.View.measure(View.java:25466)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3397)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2486)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
我这样做是因为我想在this post中应用类似点击的工具提示,它在OnClickListener
中执行perfromLongClick()
。我试图稍微改变他的方式。我计划在init{}
块中这样做:
init {
// so we can click and show its tooltip; a long-click still does
setOnClickListener { performLongClick() }
}
它与一个扩展TextView
的类一起工作。请帮助,谢谢。
我已经列出了我在上面尝试过的东西。我找不到任何与SwitchMaterial
相关的帖子或教程。也许没有必要这样做?
编辑:添加build.gradle文件
我的build.gradle
文件如下:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
compileSdk 31
defaultConfig {
applicationId "xxx"
minSdk 26
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'AndroidManifest.xml'
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation project(path: ':opencv')
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'com.google.code.gson:gson:2.10'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
implementation group: 'org.javatuples', name: 'javatuples', version: '1.2'
implementation('com.squareup.okhttp3:okhttp:4.9.3')
implementation('org.apache.commons:commons-csv:1.5')
implementation files('libs/openCVLibrary330-v1.1-release.aar')
//firebase, choose for kotlin and java or Both.
implementation 'com.google.firebase:firebase-crashlytics-ktx:18.3.0'
implementation 'com.google.firebase:firebase-crashlytics'
}
1条答案
按热度按时间ijnw1ujt1#
在系统/库代码中发生的这些类型的错误可能很难跟踪。
我只能通过1)强制
defStyleAttr
为零,或2)强制布局文件中的textOn
属性为空来重现您的错误(android:textOn="@null"
)。这告诉我你正在丢失属性或修改样式,这样textOn
和,可能,textOff
显示为null。您可以通过在XML布局中设置android:textOn="On"
和android:textOff="Off"
来确定这是问题的根源。(但这不是修复方法。)那么,我为什么要这么说呢?如果我们以相反的顺序获取堆栈跟踪中最上面的程序引用行,我们可以看到崩溃所涉及的代码。
如果我们在 * StaticLayout.java * 的第455行设置一个断点,我们可以看到
source
为null,这直接导致了崩溃。java.lang.NullPointerException:尝试对空对象引用调用接口方法“intjava.lang.CharSequence.length()”
追溯
source
变量的来源,我们看到它来自mTextOnTransformed
(SwitchCompat.java
的第905行)。mTextOnTransformed
唯一被设置的地方是在下面的代码中:而
textOn
是当开关为“on”且为null时显示的文本。我不能告诉你具体如何纠正这个错误,但仔细看看你有什么接触开关和它的属性,包括样式,试图找出哪里出了问题。