简体   繁体   English

Android Studio KT NullPointerException 错误

[英]Android Studio KT NullPointerException error

I added a few button functions to the main activivty.kt in my android studio project, and as far as I know, I initialized and called the variables.我在我的 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()
        }
 

This is the error that is given when I click the buttons in the emulator.这是我在模拟器中单击按钮时出现的错误。

    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)

Everything is imported, and there are no problems there.一切都是进口的,那里没有问题。 I understand the NullPointerException is telling me the object I am calling is null, but i'm not sure where I went wrong.我知道 NullPointerException 告诉我我正在呼叫的 object 是 null,但我不确定哪里出错了。 Help is appreciated.感谢帮助。

Okay: i'm adding the xml files.好的:我正在添加 xml 文件。

activitymain 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>

Viewpager查看页面


<?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>

The NPE is happening on a textView that you reference within the OnClickListener , but it seems it is not included in the layout file that you are referring to. NPE 发生在您在OnClickListener textView但它似乎未包含在您引用的布局文件中。 Please verify the view you would like to animate, or the layout file.请验证您想要设置动画的视图或布局文件。

I believe you are wanting to start animation on click of the buttons.我相信您希望通过单击按钮启动 animation。 You will first have to initialize these buttons and then go forth and start Animation on them.您首先必须初始化这些按钮,然后 go 并在它们上启动 Animation。 Example to start animation on one of your buttons在您的一个按钮上启动 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