Android 如何在主类内部执行片段onClick操作?

Android 如何在主类内部执行片段onClick操作?,android,kotlin,Android,Kotlin,片段onClick失败,出现以下错误 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414) at android.view.View.perf

片段onClick失败,出现以下错误

java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:7357)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:7334)
        at android.view.View.access$3600(View.java:808)
        at android.view.View$PerformClick.run(View.java:28200)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:7357) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
        at android.view.View.performClickInternal(View.java:7334) 
        at android.view.View.access$3600(View.java:808) 
        at android.view.View$PerformClick.run(View.java:28200) 
        at android.os.Handler.handleCallback(Handler.java:907) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7478) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941) 
     Caused by: java.lang.NullPointerException: farenheitView must not be null
        at com.example.MainActivity.celsiusFunction(MainActivity.kt:68)
主要活动:

以下是我的MainActivity 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_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <FrameLayout
        android:id="@+id/main_fragment_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

因此,我的应用程序布局有一个底部导航栏。每个导航栏都有自己的片段。一个是RecyclerView、TempConverter视图和ImageUploader视图。如何正确地实现片段活动以及我的代码有什么问题?

从logcat中,您可以看到它说您的farenheitView为null,这是导致错误的原因。现在,根据xml和MainActivity,很明显,片段中有farenheitView,但您正试图在MainActivity中获得它,这就是您在那里获得空值的原因

相反,您需要做的是,您只需要将片段的函数放在fragment类中,而不需要放在mainActivity类中,并且您可以在片段中需要的任何地方调用它们

我还建议您在片段中使用绑定,我可以看到您已经在mainActivity中使用了绑定

你可以了解更多关于片段的信息,还有很多其他的博客和教程


快乐编码

您好,我可以从您的日志中看到,您的fahrenhit视图为空。查看以下行:-原因:java.lang.NullPointerException:farenheitView在com.example.MainActivity.celsiusFunctionMainActivity.kt:68I假设MainActivity中的行号68是以下行:-val farenheitView=view.findViewByIdR.id.userTemp val farenheitValue=farenheitView.text.toString从何处开始调用此函数?我从我的主类调用它。请添加代码。我在MainActivity中没有看到它。另外,请检查您在xml中是否使用了相同的id。应该是userTemp bothsI,我已经在celsius函数中添加了上面的主类代码。我会再次检查我的身份证
class TempConverterFragment: Fragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = inflater.inflate(R.layout.fragment_temp_converter, container, false)
        return binding
    }
}
<?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_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <FrameLayout
        android:id="@+id/main_fragment_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
 <?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_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="487dp"
    android:layout_height="320dp"
    android:layout_marginBottom="16dp"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/termometer" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="62dp"
    android:layout_marginLeft="62dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="76dp"
    android:layout_marginRight="76dp"
    android:text="Enter desired temperature to convert"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />

<EditText
    android:id="@+id/userTemp"
    style="@android:style/Widget.DeviceDefault.EditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="140dp"
    android:layout_marginLeft="140dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="140dp"
    android:layout_marginRight="140dp"
    android:ems="10"
    android:hint="Enter Temperature Here"
    android:inputType="numberDecimal"
    android:selectAllOnFocus="false"
    android:singleLine="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
    android:id="@+id/celsiusButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="70dp"
    android:layout_marginLeft="70dp"
    android:layout_marginTop="32dp"
    android:onClick="celsiusFunction"
    android:text="to Celsius"
    app:backgroundTint="@color/purple_200"
    app:layout_constraintEnd_toStartOf="@+id/farenheitButton"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/userTemp" />

<Button
    android:id="@+id/farenheitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="41dp"
    android:layout_marginLeft="41dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="72dp"
    android:layout_marginRight="72dp"
    android:onClick= "farenheitFunction"
    android:text= "to Farenheit"
    app:backgroundTint="@color/teal_200"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/celsiusButton"
    app:layout_constraintTop_toBottomOf="@+id/userTemp" />