我正在尝试在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 中应用类似点击的工具提示,这会在
perfromLongClick()
中实现 OnClickListener
。我正在尝试稍微改变他的方式。我计划在其 init{}
块内执行此操作,如下所示:
init {
// so we can click and show its tooltip; a long-click still does
setOnClickListener { performLongClick() }
}
它适用于扩展
TextView
的类。请帮忙,谢谢。
我已经列出了我在那里尝试过的内容。我找不到任何相关的帖子或教程来扩展
SwitchMaterial
。也许没有必要这样做?
我的
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)强制
defStyleAttr
为零,或者 2)强制布局文件中的 textOn
属性为空(android:textOn="@null"
)来重现您的错误。这告诉我,您正在丢失属性或修改样式,使得 textOn
以及可能的 textOff
出现空值。通过在 XML 布局中设置 android:textOn="On"
和 android:textOff="Off"
,您也许可以确信这就是问题的根源。 (但这不是解决办法。)
那么,我为什么这么说呢?如果我们以相反的顺序获取堆栈跟踪中顶部的程序参考行,我们可以看到崩溃中涉及的代码。
at androidx.appcompat.widget.SwitchCompat.onMeasure(SwitchCompat.java:905)
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...
if (mOnLayout == null) {
mOnLayout = makeLayout(mTextOnTransformed); // Line 905
}
...
}
at androidx.appcompat.widget.SwitchCompat.makeLayout(SwitchCompat.java:982)
private Layout makeLayout(CharSequence transformedText) {
return new StaticLayout(transformedText, mTextPaint,
transformedText != null
? (int) Math.ceil(Layout.getDesiredWidth(transformedText, mTextPaint)) : 0, // Line 982
Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
}
at android.text.StaticLayout.<init>(StaticLayout.java:455)
this(source, 0, source.length(), paint, width, align, // Line 455
spacingmult, spacingadd, includepad);
如果我们在StaticLayout.java的第455行设置断点,我们可以看到
source
为空,直接导致崩溃。
java.lang.NullPointerException:尝试在空对象引用上调用接口方法“int java.lang.CharSequence.length()”
追溯到
source
变量的来源,我们看到它来自mTextOnTransformed
(SwitchCompat.java
的第905行。)唯一设置mTextOnTransformed
的地方是在以下代码中:
In SwtichCompat.hava:
private void setTextOnInternal(CharSequence textOn) {
...
mTextOnTransformed = doTransformForOnOffText(textOn);
...
}
并且
textOn
是开关“打开”时显示的文本,为空。
我无法具体告诉您如何纠正此错误,但请仔细查看与开关及其属性(包括样式)相关的所有内容,以尝试找出问题所在。
你可以发布你的themes.xml文件(或styles.xml)吗?
我遇到了类似的问题,经过调查,我发现我的主要应用程序主题是扩展 Material 2 样式。我的主要应用程序主题如下:
<!-- Base application theme. -->
<style name="Theme.AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
...
</style>
更改为扩展 Material 3 样式后,我能够修复它。
<!-- Base application theme. -->
<style name="Theme.AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
...
</style>