kotlin 空指针异常线性布局

eufgjt7s  于 2023-01-09  发布在  Kotlin
关注(0)|答案(1)|浏览(171)

我遇到了一些NullPointerExceptions的问题,但我不知道如何修复它

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.bputs.MainActivity.setupUI(MainActivity.kt:63)
        at com.example.bputs.MainActivity.onCreate(MainActivity.kt:31)

我发现问题出在MainActivity上,但请告诉我如何更改它,下面是MainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var productViewModel: ProductViewModel
    private lateinit var bottomSheet : BottomSheetBehavior<*>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportActionBar?.hide()
        setupUI()
    }

    private fun setupUI() {
        productViewModel = ViewModelProvider(this).get(ProductViewModel::class.java)
        rv_main_product.apply {
            layoutManager = if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
                GridLayoutManager(this@MainActivity, 2)
            } else {
                GridLayoutManager(this@MainActivity,4)
            }
            adapter = ProductsAdapter(mutableListOf(), this@MainActivity, productViewModel)
        }

        rv_selected_produk.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter = SelectedProductAdapter(mutableListOf(), this@MainActivity, productViewModel)
        }

        bottomSheet = BottomSheetBehavior.from(detail_order)
        bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
        detailBtn.setOnClickListener {
            if (bottomSheet.state == BottomSheetBehavior.STATE_COLLAPSED || bottomSheet.state == BottomSheetBehavior.STATE_HIDDEN) {
                bottomSheet.state = BottomSheetBehavior.STATE_EXPANDED
            } else {
                bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
            }
        }

        productViewModel.fetchDummy()
        productViewModel.listenToProducts().observe(this, Observer {
            rv_main_product.adapter?.let { p ->
                if (p is ProductsAdapter) {
                    p.updateList(it)
                }
            }
        })

这是XML页面

<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity"
    android:background="@color/warnaUtama">

    <LinearLayout
        android:id="@+id/layout_atas"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/logo"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="18dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/poppinssemibold"
            android:layout_centerHorizontal="true"
            android:text="Beautyper"
            android:textSize="38dp"
            android:layout_marginTop="20dp"
            android:paddingLeft="25dp"
            android:textColor="@color/warnaKedua"/>
    </LinearLayout>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_main_product"
            android:layout_width="match_parent"
            android:layout_height="560dp"
            android:layout_marginTop="100dp"
            android:padding="8dp"/>

    <include layout="@layout/cart"/>
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="10dp"
        android:weightSum="2"
        android:layout_gravity="bottom"
        android:background="@drawable/btn_shape">
        <TextView
            android:id="@+id/checkoutBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Checkout"
            android:paddingStart="16dp"
            android:padding="8dp"
            android:fontFamily="@font/poppinsbold"
            android:textColor="@color/white"
            android:layout_weight="1.5"/>

        <ImageView
            android:id="@+id/detailBtn"
            android:layout_gravity="center_vertical"
            android:layout_width="20dp"
            android:layout_height="35dp"
            android:layout_marginRight="10dp"
            android:layout_weight="0.5"
            android:src="@drawable/ic_baseline_expand_less_24"/>
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

请帮助我解决这个问题,我是新手,所以现在我卡住了。我可以用LinearLayout做什么?

46qrfjad

46qrfjad1#

我不太理解你的代码,因为你删除了一些行,我觉得这会帮助我理解更多。如果从日志中我可以理解,问题可能是由于2件事,我想象:

  • 您可能把detailBtn的id放错了,因为日志说您的LinearLayout包含一个ClickListener事件,但是我在代码中没有看到它
  • 当片段处于OnCreate之外的另一个状态时,您调用了setOnClickListener,我不确定bottomSheet是否更改了此状态,但这也可能是问题所在。因此,应用可能找不到视图。

相关问题