gpt4 book ai didi

java - Android 应用程序在多次切换 fragment 时崩溃

转载 作者:行者123 更新时间:2023-12-02 09:14:07 27 4
gpt4 key购买 nike

正如您从标题中看到的那样,当我在 fragment/选项卡之间切换时,我的应用程序崩溃了。我有 3 个选项卡,如果我在第一个和第二个选项卡之间切换多次,什么也不会发生,如果我对第三个和第二个选项卡或第三个选项卡和第二个选项卡和第一个选项卡执行相同的操作,应用程序就会崩溃。

如果需要超过以下代码和异常,请告诉我。

异常(exception):

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)

我的 fragment 类:

  1. fragment/选项卡(这不是整个类):
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
}
  • fragment/选项卡(这不是完整的类):

  • 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()


    }
    }




  • fragment/选项卡:

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

    最佳答案

    OnItemSelectedListener 中的 view: View 更改为可为空的 view: View?

    它是一个具有未指定可空性的框架 java 接口(interface),因此 kotlin 编译器允许您使用不同的非空类型实现该接口(interface)。然而,在运行时,可以使用 null 参数调用该函数,当 kotlin 运行时检查非空值实际上不为空时,会导致崩溃。

    关于java - Android 应用程序在多次切换 fragment 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59142686/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com