简体   繁体   中英

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.


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. Help is appreciated.

Okay: i'm adding the xml files.

activitymain 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. 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. You will first have to initialize these buttons and then go forth and start Animation on them. Example to start animation on one of your buttons

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()
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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