Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin程序在将clickListener设置为膨胀XML中定义的按钮时抛出nullPointerException,不确定要转到哪里_Android_Xml_Kotlin_Button - Fatal编程技术网

Android Kotlin程序在将clickListener设置为膨胀XML中定义的按钮时抛出nullPointerException,不确定要转到哪里

Android Kotlin程序在将clickListener设置为膨胀XML中定义的按钮时抛出nullPointerException,不确定要转到哪里,android,xml,kotlin,button,Android,Xml,Kotlin,Button,我试图自学Android开发和Kotlin,我已经在这个问题上纠缠了一段时间。因为我是一个比较新的人,我几乎可以肯定这只是我不理解的事情,我真的非常感谢任何帮助。parameterize_按钮在fragment_仪表板XML文件中定义。此XML在DashboardFragment.kt中膨胀,但仍抛出: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.caelussidekick, PID: 15355 j

我试图自学Android开发和Kotlin,我已经在这个问题上纠缠了一段时间。因为我是一个比较新的人,我几乎可以肯定这只是我不理解的事情,我真的非常感谢任何帮助。parameterize_按钮在fragment_仪表板XML文件中定义。此XML在DashboardFragment.kt中膨胀,但仍抛出:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.caelussidekick, PID: 15355
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.caelussidekick.ui.dashboard.DashboardFragment.onCreateView
这是我的XML,我知道它是parameterize_按钮,因为我没有将任何onClickListeners设置为任何其他按钮

<?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=".ui.dashboard.DashboardFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/parameterize_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/parameters_buttion" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/outputs_header"
                android:textAlignment="center"
                android:textSize="20sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/dv_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/empty_string"
                    android:textAlignment="textEnd" />

                <TextView
                    android:id="@+id/dv_unit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/dv"
                    android:textAlignment="textStart" />

                <Button
                    android:id="@+id/dv_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/dv_button" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/isp_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/empty_string"
                    android:textAlignment="textEnd"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/isp_unit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/isp"
                    android:textAlignment="textStart" />

                <Button
                    android:id="@+id/isp_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/isp_button" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

谢谢你的帮助!老实说,我真是不知所措。我广泛地研究了stackoverflow,并尝试将按钮移动到主活动,使用可为空的修饰符,并将其作为类中的字段,但是什么都没有起作用。

合成视图绑定在onCreateView返回之前是无效的-在它实际设置为片段上的视图之前,它不知道您的输出视图

因此,您可以将代码移动到onViewCreated,这是在onCreateView之后立即发生的方法。通常,您不应该做任何事情,而是在onCreateView中膨胀视图:

parmetrizedbtn.setOnClickLIstener{ val intent=Intentview.context,ParamActivity::class.java 星际维京帐篷 } 可能对你的工作有用

package com.example.caelussidekick.ui.dashboard

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.caelussidekick.R
import androidx.appcompat.app.AppCompatActivity
import android.net.Uri
import kotlinx.android.synthetic.main.fragment_dashboard.*

class DashboardFragment : Fragment() {


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
//        dashboardViewModel =
//                ViewModelProviders.of(this).get(DashboardViewModel::class.java)
        val output = inflater.inflate(R.layout.fragment_dashboard, container, false)


//        val textView: TextView = root.findViewById(R.id.text_dashboard)
//        dashboardViewModel.text.observe(viewLifecycleOwner, Observer {
//            textView.text = it
//        })
        this.parameterize_button.setOnClickListener{
            val intent = Intent(activity, ParamActivity::class.java)
            startActivity(intent)
        }

        return output

    }


}

class DashboardFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_dashboard, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        this.parameterize_button.setOnClickListener{
            val intent = Intent(activity, ParamActivity::class.java)
            startActivity(intent)
        }
    }
}
var parametrizedbtn = view.findViewById<Button>(R.id.parameterize_button)