正如您已经从标题中看到的那样,当我在片段/标签之间切换时,我的应用程序崩溃了。我有3个标签,如果我在第一个和第二个之间切换,那么什么也没有发生,如果我对第三个和第二个或第三个到第二个做同样的操作,那么应用程序崩溃。
如果需要以下代码和例外,请告诉我。
例外:
E/AndroidRuntime:
FATAL EXCEPTION: main
Process: de.noel.networkscanner,
PID: 8493
java.lang.IllegalArgumentException:
Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, Parameter view
at de.noel.networkscanner.ui.main.FragmentDownlaod$registerListener$1.onItemSelected(Unknown
Source:7)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:1366)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:1355)
at android.widget.AdapterView.selectionChanged(AdapterView.java:1339)
at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1530)
at android.widget.AdapterView.handleDataChanged(AdapterView.java:1506)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:191)
at android.widget.Spinner.onMeasure(Spinner.java:631)
at androidx.appcompat.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:428)
at android.view.View.measure(View.java:24966)
at android.widget.GridLayout.measureChildWithMargins2(GridLayout.java:1018)
at android.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:1028)
at android.widget.GridLayout.onMeasure(GridLayout.java:1067)
at android.view.View.measure(View.java:24966)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:978)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:24966)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1638)
at android.view.View.measure(View.java:24966)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:978)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:24966)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:24966)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:24966)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:24966)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:24966)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:992)
at android.view.View.measure(View.java:24966)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3301)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2028)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2330)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1888)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8511)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native
Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
我的片段类:
class FragmentDownlaod : Fragment {
constructor() : super()
private lateinit var myView: View
private var sizeInBit = true
private var speedInBit = true
private var selectedSizeForDownloadSize = DataSizeNumbers.KILO
private var selectedSizeForDownloadSpeed = DataSizeNumbers.KILO
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
updateSpinnerItems()
this.speedSpinner.setSelection(1)
this.sizeSpinner.setSelection(1)
registerListener()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
this.myView = inflater.inflate(R.layout.download_fragement, container, false)
return this.myView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.context)
val activity = this.activity
if(activity != null) {
val display: Display = activity.windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width = size.x
this.bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888)
this.shapeDrawable = ShapeDrawable()
}
}
片段/标签:
class FragmentConverter : Fragment {
constructor() : super()
private lateinit var myView: View
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
this.myView = inflater.inflate(R.layout.converter_fragement, container, false)
return this.myView
}
编辑:添加了registerListener方法:
private fun registerListener() {
textEditSize.addTextChangedListener(TextChangeWatcher(textEditSize))
textEditSpeed.addTextChangedListener(TextChangeWatcher(textEditSpeed))
sizeSpinner.onItemSelectedListener = (object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
when (position) {
0 -> selectedSizeForDownloadSize = DataSizeNumbers.BIT
1 -> selectedSizeForDownloadSize = DataSizeNumbers.KILO
2 -> selectedSizeForDownloadSize = DataSizeNumbers.MEGA
3 -> selectedSizeForDownloadSize = DataSizeNumbers.GIGA
4 -> selectedSizeForDownloadSize = DataSizeNumbers.TERRA
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
selectedSizeForDownloadSize = DataSizeNumbers.KILO
}
})
speedSpinner.onItemSelectedListener = (object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
when (position) {
0 -> selectedSizeForDownloadSpeed = DataSizeNumbers.BIT
1 -> selectedSizeForDownloadSpeed = DataSizeNumbers.KILO
2 -> selectedSizeForDownloadSpeed = DataSizeNumbers.MEGA
3 -> selectedSizeForDownloadSpeed = DataSizeNumbers.GIGA
4 -> selectedSizeForDownloadSpeed = DataSizeNumbers.TERRA
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
selectedSizeForDownloadSpeed = DataSizeNumbers.KILO
}
})
calcBtn.setOnClickListener {
if (editTextIncludesNumbers(textEditSize)) {
if (editTextIncludesNumbers(textEditSpeed)) {
speedResultView.text = Calculator.calculateTime(
getNumberFromField(textEditSize),
selectedSizeForDownloadSize,
sizeInBit,
getNumberFromField((textEditSpeed)),
selectedSizeForDownloadSpeed,
speedInBit
)
} else {
speedResultView.text = getString(R.string.noCorrectNumber_speed)
}
} else {
Log.d("edit", "is empty -> ${getString(R.string.noCorrectNumber_size)}")
speedResultView.text = getString(R.string.noCorrectNumber_size)
}
}
sizeBitByteSwitcher.setOnCheckedChangeListener { _, _ ->
sizeInBit = !sizeInBit
updateSpinnerItems()
}
speedBitByteSwitcher.setOnCheckedChangeListener { _, _ ->
speedInBit = !speedInBit
updateSpinnerItems()
}
}
答案 0 :(得分:1)
将view: View
中的OnItemSelectedListener
更改为可为空的view: View?
。
这是具有未指定可空性的框架Java接口,因此kotlin编译器允许您使用其他nonnull类型实现该接口。但是,在运行时可以使用null参数调用该函数,从而在kotlin运行时检查您的非null实际上不是null时导致崩溃。