繁体   English   中英

Android Studio KT NullPointerException 错误

[英]Android Studio KT NullPointerException error

我在我的 android 工作室项目中的主要 activivty.kt 添加了几个按钮功能,据我所知,我初始化并调用了变量。


package com.dev.afp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.Toast
import android.widget.Toast.makeText
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.splashscreen.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val images = listOf(
            R.drawable.thumbnail_image001,
            R.drawable.image00,
            R.drawable.image01,
            R.drawable.image03,
            R.drawable.image04,
            R.drawable.image10,
            R.drawable.image13
        )

        val adapter = ViewPagerAdapter(images)
        viewPager.adapter = adapter

        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            tab.text = "${position + 1}"
        }.attach()

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                makeText(
                    this@MainActivity,
                    "Reselected Tool Segment ${tab?.text}",
                    Toast.LENGTH_SHORT
                ).show()
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                makeText(
                    this@MainActivity,
                    "Unselected Tool Segment ${tab?.text}",
                    Toast.LENGTH_SHORT
                ).show()
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                makeText(
                    this@MainActivity,
                    "Selected Tool Segment ${tab?.text}",
                    Toast.LENGTH_SHORT
                ).show()
            }
        })
        btnZoomIn.setOnClickListener {
            val animation = AnimationUtils.loadAnimation(this,R.anim.zoom_in)
            textView.startAnimation(animation)
            makeText(this@MainActivity, "Zoomed In", Toast.LENGTH_SHORT).show()
        }
        btnZoomOut.setOnClickListener {
            val animation = AnimationUtils.loadAnimation(this,R.anim.zoom_out)
            textView.startAnimation(animation)
            makeText(this@MainActivity, "Zoomed Out", Toast.LENGTH_SHORT).show()
        }
        slide_down.setOnClickListener {
            val animation = AnimationUtils.loadAnimation(this,R.anim.slide_down)
            textView.startAnimation(animation)
            makeText(this@MainActivity, "Moving Down", Toast.LENGTH_SHORT).show()
        }
        slide_up.setOnClickListener {
            val animation = AnimationUtils.loadAnimation(this,R.anim.slide_up)
            textView.startAnimation(animation)
            makeText(this@MainActivity, "Moving Up", Toast.LENGTH_SHORT).show()
        }
 

这是我在模拟器中单击按钮时出现的错误。

    Process: com.dev.afp, PID: 13840
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.startAnimation(android.view.animation.Animation)' on a null object reference
        at com.dev.afp.MainActivity$onCreate$1.onClick(MainActivity.kt:21)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        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)

一切都是进口的,那里没有问题。 我知道 NullPointerException 告诉我我正在呼叫的 object 是 null,但我不确定哪里出错了。 感谢帮助。

好的:我正在添加 xml 文件。

活动主要 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"
    android:orientation="vertical"
    android:background="@color/red"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/slide_up"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginTop="4dp"
        android:layout_weight="1"
        android:baselineAligned="false"
        android:background="#000000"
        android:text="Slide Up"
        app:layout_constraintStart_toEndOf="@+id/btnZoomOut"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnZoomIn"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:text="Zoom In"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/red"
        app:layout_constraintTop_toBottomOf="@+id/btnZoomIn" />

    <Button
        android:id="@+id/btnZoomOut"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginTop="4dp"
        android:text="@string/zoom_out"
        app:layout_constraintStart_toEndOf="@+id/btnZoomIn"
        android:background="#000000"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/slide_down"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginStart="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="4dp"
        android:text="@string/slide_down"
        android:background="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.971"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="25dp" />


</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">

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="384dp"
        android:layout_height="395dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

NPE 发生在您在OnClickListener textView但它似乎未包含在您引用的布局文件中。 请验证您想要设置动画的视图或布局文件。

我相信您希望通过单击按钮启动 animation。 您首先必须初始化这些按钮,然后 go 并在它们上启动 Animation。 在您的一个按钮上启动 animation 的示例

val btnZoomIn = findViewById<Button>(R.id.btnZoomIn)
btnZoomIn.setOnClickListener {
            val animation = AnimationUtils.loadAnimation(this,R.anim.zoom_in)
            btnZoomIn.startAnimation(animation)
            makeText(this@MainActivity, "Zoomed In", Toast.LENGTH_SHORT).show()
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM