java—我编写了一个代码,用于从一个单独的活动中动态创建主页上的新按钮,但当我单击按钮时,整个应用程序就会崩溃

7d7tgy0s  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(359)

我正在尝试做一个测验应用程序,如果你点击主页上的加号按钮,它会把你带到另一个页面,在那里你可以标题你的测验。一旦你输入了标题,你点击复选标记按钮,它会带你回到主页,新的测验将出现与用户输入的标题。这是到目前为止我的第二个活动页面中的内容,您可以在其中创建测验的标题:

package com.example.k_ari

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import com.google.android.material.floatingactionbutton.FloatingActionButton

class Page2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_page2)

        val button = Button(this@Page2)
        button.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)

        val message = "New quiz created"
        val toast1= Toast.makeText(applicationContext, message, Toast.LENGTH_LONG)
        toast1.setGravity(Gravity.CENTER, 0,0)

        val layout = findViewById<LinearLayout>(R.id.mainlayout)

        val btn = findViewById<FloatingActionButton>(R.id.floatingActionButton2)

        btn.setOnClickListener{

            layout.addView(button)

            toast1.show()

            val intent = Intent(this@Page2, MainActivity::class.java)
            startActivity(intent)
        }
    }
}

这是主要的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/mainlayout"
    android:orientation="vertical"
    android:layout_gravity="center"
    tools:context=".MainActivity">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="56dp"
        android:backgroundTint="#250A43"
        android:backgroundTintMode="add"
        android:clickable="true"
        android:contentDescription="@string/todo"
        android:tint="#250A43"
        android:translationX="280dp"
        android:translationY="580dp"
        app:backgroundTint="#250A43"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rippleColor="#FFFFFF"
        app:srcCompat="@android:drawable/ic_input_add" />
</LinearLayout>

这是第二个活动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:id="@+id/secondlayout"
    tools:context=".Page2">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#99141414"
        android:backgroundTint="#180030"
        android:padding="20dp"
        android:text="Enter the title:"
        android:textAlignment="center"
        android:textColor="#DADADA"
        android:textSize="35sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.433"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.112" />

    <EditText
        android:id="@+id/txt"
        android:layout_width="361dp"
        android:layout_height="67dp"
        android:autofillHints="title"
        android:background="#99141414"
        android:backgroundTint="#180030"
        android:hint="title"
        android:inputType="text"
        android:padding="10dp"
        android:textColor="#DADADA"
        android:textColorHint="#9ADCDCDC"
        android:textSize="28sp"
        android:translationY="170dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:contentDescription="@string/todo"
        android:translationY="600dp"
        app:backgroundTint="#180030"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.9"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是日志:

2020-12-22 12:13:18.614 24120-24120/? E/Toast: setGravity() shouldn't be called on text toasts, the values won't be used
2020-12-22 12:13:19.631 24120-24120/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.k_ari, PID: 24120
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
        at com.example.k_ari.Page2$onCreate$1.onClick(Page2.kt:30)
        at android.view.View.performClick(View.java:8178)
        at android.view.View.performClickInternal(View.java:8147)
        at android.view.View.access$3700(View.java:888)
        at android.view.View$PerformClick.run(View.java:30233)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8414)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
dz6r00yl

dz6r00yl1#

根据此修改代码并选中:
您使用“this”作为click listener的引用,而不是activity的引用。
按钮的父布局是linearlayout,因此将viewgroup替换为linearlayout
在click侦听器外部获取button和toast的引用,因为不需要每次单击时都创建。
修改代码:

val button = Button(this@Page2)
button.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)

val Message = "New quiz created"
val toast1= Toast.makeText(applicationContext, Message, Toast.LENGTH_LONG)
toast1.setGravity(Gravity.CENTER, 0,0)

val layout = findViewById<LinearLayout>(R.id.mainlayout)

val btn = findViewById<FloatingActionButton>(R.id.floatingActionButton2)

        btn.setOnClickListener{

            layout.addView(button)

            toast1.show()

            val intent = Intent(this@Page2, MainActivity::class.java)
            startActivity(intent)
        }

相关问题