【发布时间】:2021-02-23 21:11:30
【问题描述】:
我在我的android studio项目的主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 告诉我我正在调用的对象为空,但我不确定我哪里出错了。感谢您的帮助。
好的:我正在添加 xml 文件。
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>
浏览器
<?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>
【问题讨论】:
-
如果您提供失败的行的内容会很有帮助:
at com.dev.afp.MainActivity$onCreate$1.onClick(MainActivity.kt:21)。此外,如果您可以共享整个onCreate方法,它也会提供一些上下文。 -
我们还需要查看xml文件
-
这就是不鼓励合成视图引用的原因。我推荐视图绑定。 developer.android.com/topic/libraries/view-binding
标签: java android android-studio user-interface kotlin