Android 应用程序不会启动,也不会列出错误/警告。使用片段和问题似乎与 var args = ViewFragmentArgs

Posted

技术标签:

【中文标题】Android 应用程序不会启动,也不会列出错误/警告。使用片段和问题似乎与 var args = ViewFragmentArgs【英文标题】:Android app won't start and doesn't list errors/warnings. Using fragments and issue seems to be with var args = ViewFragmentArgs 【发布时间】:2021-12-20 20:47:58 【问题描述】:

我正在尝试构建一个如下所示的计算器应用程序: image of calculator

显示部分和键盘部分是独立的片段,键盘片段需要更新显示片段。

已根据 Jetpack 更新 gradle 文件,以实现安全 args 和导航。另外,我正在使用视图绑定,并且所有内容都已相应更新。

以下代码用于将信息发送到显示文件的片段键盘文件(我认为这部分没问题,但我可能对最后一行有问题,因为它将数据发送到显示文件 -> findNavController().navigate(KeysFragmentDirections.actionKeysFragmentToViewFragment(completeNumber)))

package com.test.cis2818_proj2.ui.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.test.cis2818_proj2.databinding.FragmentKeysBinding

class KeysFragment : Fragment() 
    private lateinit var binding: FragmentKeysBinding
    var completeNumber = ""
    var operand1 = 0.0f
    var operand2 = 0.0f
    var operation = ""

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? 

        binding = FragmentKeysBinding.inflate(inflater)

        binding.keysFragmentBt0.setOnClickListener  createNumber("0") 
        binding.keysFragmentBt1.setOnClickListener  createNumber("1") 
        binding.keysFragmentBt2.setOnClickListener  createNumber("2") 
        binding.keysFragmentBt3.setOnClickListener  createNumber("3") 
        binding.keysFragmentBt4.setOnClickListener  createNumber("4") 
        binding.keysFragmentBt5.setOnClickListener  createNumber("5") 
        binding.keysFragmentBt6.setOnClickListener  createNumber("6") 
        binding.keysFragmentBt7.setOnClickListener  createNumber("7") 
        binding.keysFragmentBt8.setOnClickListener  createNumber("8") 
        binding.keysFragmentBt9.setOnClickListener  createNumber("9") 
        binding.keysFragmentBtDot.setOnClickListener  createNumber(".") 

        return binding.root
    

    private fun createNumber(stringNum: String) 
        if (completeNumber == "" && stringNum == ".") 
            completeNumber = "0"
        

        if (completeNumber == "" && stringNum == "0") 
            completeNumber = ""
         else 
            completeNumber += stringNum
        

        binding.keysFragmentTvTester.text = completeNumber

       findNavController().navigate(KeysFragmentDirections.actionKeysFragmentToViewFragment(completeNumber))

    

这是显示文件的所有代码:

package com.test.cis2818_proj2.ui.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.test.cis2818_proj2.databinding.FragmentViewBinding

class ViewFragment : Fragment() 
    private lateinit var binding: FragmentViewBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? 
        binding = FragmentViewBinding.inflate(inflater)

        var args = ViewFragmentArgs.fromBundle(requireArguments())
        binding.viewFragmentTvDisplay.text = args.displayData

        return binding.root
    


问题似乎与以下代码有关:

    var args = ViewFragmentArgs.fromBundle(requireArguments())
    binding.viewFragmentTvDisplay.text = args.displayData

当我删除此代码时,应用程序运行并且看起来很好。使用应用程序中的代码,它根本无法启动并崩溃。

感谢任何帮助了解我的问题所在!

navigation.xml 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navigation"
    app:startDestination="@id/keysFragment">

    <fragment
        android:id="@+id/keysFragment"
        android:name="com.test.cis2818_proj2.ui.main.KeysFragment"
        android:label="fragment_keys"
        tools:layout="@layout/fragment_keys" >
        <action
            android:id="@+id/action_keysFragment_to_viewFragment"
            app:destination="@id/viewFragment" />
    </fragment>
    <fragment
        android:id="@+id/viewFragment"
        android:name="com.test.cis2818_proj2.ui.main.ViewFragment"
        android:label="fragment_view"
        tools:layout="@layout/fragment_view" >
        <argument
            android:name="display_data"
            app:argType="string"
            app:nullable="true" />
    </fragment>
</navigation>

来自 logcat 的错误(不确定这是否与堆栈跟踪相同...抱歉)

2021-11-07 15:42:59.034 20955-20955/com.test.cis2818_proj2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.cis2818_proj2, PID: 20955
    java.lang.IllegalStateException: Fragment ViewFragmentf3bb415 (4c06d0b7-8565-4b91-b844-7e691229f22c id=0x7f0a0106) does not have any arguments.
        at androidx.fragment.app.Fragment.requireArguments(Fragment.java:748)
        at com.test.cis2818_proj2.ui.main.ViewFragment.onCreateView(ViewFragment.kt:19)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8018)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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)

这里是main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/container"
    android:layout_
    android:layout_
    android:layout_margin="5dp"
    android:background="@color/desert_sand"
    android:padding="10dp"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/main_activity_ct_view_fragment"
        android:name="com.test.cis2818_proj2.ui.main.ViewFragment"
        android:layout_
        android:layout_
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/fragment_view" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView2"
        android:name="com.test.cis2818_proj2.ui.main.KeysFragment"
        android:layout_
        android:layout_
        android:layout_marginTop="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_activity_ct_view_fragment"
        app:layout_constraintVertical_bias="0.0"
        tools:layout="@layout/fragment_keys" />
</androidx.constraintlayout.widget.ConstraintLayout>

这里是 MainActivity.kt:

package com.test.cis2818_proj2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() 

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
    

另外,这里是 frament_keys.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_
    android:layout_
    android:background="@color/desert_sand"
    tools:context=".ui.main.KeysFragment">

    <Button
        android:id="@+id/keys_fragment_bt_1"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_1"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_clear_entry"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_clear_entry"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_clear_entry" />

    <Button
        android:id="@+id/keys_fragment_bt_2"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_2"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_1"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_1"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_1" />

    <Button
        android:id="@+id/keys_fragment_bt_3"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_3"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_2"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_subtract"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_2"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_2" />

    <Button
        android:id="@+id/keys_fragment_bt_8"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_8"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_7"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_9"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_7"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_7" />

    <Button
        android:id="@+id/keys_fragment_bt_mult"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_multiply"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_6"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_6" />

    <Button
        android:id="@+id/keys_fragment_bt_6"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_6"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_5"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_mult"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_5"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_5" />

    <Button
        android:id="@+id/keys_fragment_bt_7"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_7"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_modulus"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_8"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_modulus"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_modulus" />

    <Button
        android:id="@+id/keys_fragment_bt_div"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_divide"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_9"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_9" />

    <Button
        android:id="@+id/keys_fragment_bt_4"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_4"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_sqrt"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_sqrt"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_sqrt" />

    <Button
        android:id="@+id/keys_fragment_bt_sqrt"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_sqrt"
        android:textAlignment="center"
        android:textSize="20sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toTopOf="@+id/keys_fragment_bt_clear_entry"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/keys_fragment_bt_modulus"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_modulus"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toTopOf="@+id/keys_fragment_bt_sqrt"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_7"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/keys_fragment_bt_5"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_5"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_4"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_6"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_4"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_4" />

    <Button
        android:id="@+id/keys_fragment_bt_9"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_9"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_8"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_div"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_8"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_8" />

    <Button
        android:id="@+id/keys_fragment_bt_dot"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_dot"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_0"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_neg_sign"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_0"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_0" />

    <Button
        android:id="@+id/keys_fragment_bt_equals"
        android:layout_
        android:layout_
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_equals"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/keys_fragment_bt_clear_all" />

    <Button
        android:id="@+id/keys_fragment_bt_add"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_add"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_neg_sign"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_neg_sign"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_neg_sign" />

    <Button
        android:id="@+id/keys_fragment_bt_clear_all"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_clear_all"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_0"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/keys_fragment_bt_clear_entry" />

    <Button
        android:id="@+id/keys_fragment_bt_0"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_0"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_clear_all"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_dot"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_clear_all"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_clear_all" />

    <Button
        android:id="@+id/keys_fragment_bt_neg_sign"
        android:layout_
        android:layout_
        android:backgroundTint="@color/rich_black_fogra_29"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_neg_sign"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_dot"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_add"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_dot"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_dot" />

    <Button
        android:id="@+id/keys_fragment_bt_clear_entry"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_clear_entry"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/keys_fragment_bt_1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/keys_fragment_bt_subtract"
        android:layout_
        android:layout_
        android:backgroundTint="@color/dark_electric_blue"
        android:fontFamily="@font/squada_one"
        android:padding="5dp"
        android:text="@string/button_subtract"
        android:textAlignment="center"
        android:textSize="24sp"
        app:cornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="@+id/keys_fragment_bt_3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/keys_fragment_bt_3"
        app:layout_constraintTop_toTopOf="@+id/keys_fragment_bt_3" />

    <TextView
        android:id="@+id/keys_fragment_tv_tester"
        android:layout_
        android:layout_
        android:text="@string/tester"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/keys_fragment_bt_equals" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

那么崩溃信息是什么?请包含整个堆栈跟踪。 对不起,我是新来的。堆栈跟踪是 logcat 吗? 是的,你得到了正确的堆栈跟踪。从该消息看来,您已直接或通过您的布局 XML 将 ViewFragment 直接添加到您的 Activity 的 supportFragmentManager 中,与对 navigate() 的调用完全分开,这将用您的 @987654334 替换您的 KeysFragment @。那是你在做什么?能否包含您的活动代码和活动的布局 XML? 如果我理解你的问题...是的。我没有将 KeysFragment 替换为 ViewFragment,而是将两者都添加到 main_activity.xml 并尝试直接更新 ViewFragment。 【参考方案1】:

Navigation Component 涉及在您的 Activity 中使用单个 NavHostFragment 来确定需要使用导航图显示的片段。每次调用navigate() 都会用新片段替换当前片段。

您没有在活动 XML 中使用NavHostFragment,并且实际上不希望将一个片段替换为另一个片段。相反,您的两个片段是兄弟片段 - 两个独立添加的片段,它们都存在于您的活动布局中。这不是你会使用 Navigation、它的导航图或 Safe Args 的东西。

相反,您想Communicate between fragments using the Fragment Result API。此 API 专门用于在 Fragment 之间发送信息 - 例如在您的 ViewFragment 和 KeyFragment 之间。

这意味着您的 ViewFragment 看起来更像:

class ViewFragment : Fragment() 
    private lateinit var binding: FragmentViewBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? 
        binding = FragmentViewBinding.inflate(inflater)

        // Uses the Kotlin extension available in fragment-ktx
        setFragmentResultListener("keySelected")  _, bundle ->
            val displayData = bundle.getString("displayData")
            binding.viewFragmentTvDisplay.text = args.displayData
        

        return binding.root
    


您的KeysFragmentcreateNumber 看起来更像:

private fun createNumber(stringNum: String) 
    if (completeNumber == "" && stringNum == ".") 
        completeNumber = "0"
    

    if (completeNumber == "" && stringNum == "0") 
        completeNumber = ""
     else 
        completeNumber += stringNum
    

    binding.keysFragmentTvTester.text = completeNumber

    // Uses the Kotlin extension in fragment-ktx
    setFragmentResult("keySelected", bundleOf("displayData" to completeNumber))


【讨论】:

以上是关于Android 应用程序不会启动,也不会列出错误/警告。使用片段和问题似乎与 var args = ViewFragmentArgs的主要内容,如果未能解决你的问题,请参考以下文章

npx react-native run-android 不会启动模拟器,如果模拟器正在运行,也不会运行

Android 应用程序不会在启动时启动

Android 应用程序不会启动 noClassDefFoundError

即使地图显示没有任何错误,onMapReady 也不会调用

Android Emulator 不会运行从 Eclipse 启动的应用程序

Android:单击按钮时默认选项卡式活动不会启动