Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每当使用Intent时从Java活动启动Kotlin活动时,应用程序就会崩溃_Java_Android_Android Studio_Kotlin_Android Intent - Fatal编程技术网

每当使用Intent时从Java活动启动Kotlin活动时,应用程序就会崩溃

每当使用Intent时从Java活动启动Kotlin活动时,应用程序就会崩溃,java,android,android-studio,kotlin,android-intent,Java,Android,Android Studio,Kotlin,Android Intent,此应用程序目前有三个活动,一个登录活动、注册活动和一个用Kotlin编写的称为Bookjamroom活动的活动。Bookjamroom活动应该在单击登录按钮时显示,但每当我这样做时,应用程序就会强制关闭 记录Cat错误: - 2020-10-15 20:17:53.047 12954-12981/com.example.jamsecure E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da 2020-10-15 2

此应用程序目前有三个活动,一个登录活动、注册活动和一个用Kotlin编写的称为Bookjamroom活动的活动。Bookjamroom活动应该在单击登录按钮时显示,但每当我这样做时,应用程序就会强制关闭

记录Cat错误:

 - 2020-10-15 20:17:53.047 12954-12981/com.example.jamsecure
   E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
   2020-10-15 20:17:53.047 12954-12981/com.example.jamsecure
   E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
   2020-10-15 20:17:53.303 12954-12981/com.example.jamsecure
   D/EGL_emulation: eglMakeCurrent: 0xebb840c0: ver 3 1 (tinfo
   0xebb832e0) 2020-10-15 20:18:03.883 12954-12954/com.example.jamsecure
   D/AndroidRuntime: Shutting down VM 2020-10-15 20:18:03.889
   12954-12954/com.example.jamsecure E/AndroidRuntime: FATAL EXCEPTION:
   main
       Process: com.example.jamsecure, PID: 12954
       java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jamsecure/com.example.jamsecure.Bookjamroom}:
   java.lang.NullPointerException: Attempt to invoke virtual method
   'android.content.pm.ApplicationInfo
   android.content.Context.getApplicationInfo()' on a null object
   reference
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
           at android.app.ActivityThread.-wrap11(Unknown Source:0)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
           at android.os.Handler.dispatchMessage(Handler.java:106)
           at android.os.Looper.loop(Looper.java:164)
           at android.app.ActivityThread.main(ActivityThread.java:6494)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo
   android.content.Context.getApplicationInfo()' on a null object
   reference
           at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
           at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
           at android.content.Context.obtainStyledAttributes(Context.java:655)
           at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
           at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
           at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
           at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
           at com.example.jamsecure.Bookjamroom.<init>(Bookjamroom.kt:12)
           at java.lang.Class.newInstance(Native Method)
           at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
           at android.app.ActivityThread.-wrap11(Unknown Source:0) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
           at android.os.Handler.dispatchMessage(Handler.java:106) 
           at android.os.Looper.loop(Looper.java:164) 
           at android.app.ActivityThread.main(ActivityThread.java:6494) 
           at java.lang.reflect.Method.invoke(Native Method) 
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Kotlin活动布局的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:background="@drawable/back3"
    tools:context=".Bookjamroom">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bfa"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.955"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.976"
        app:srcCompat="@drawable/b_add" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bnj"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/bfa"
        app:layout_constraintEnd_toEndOf="@+id/bfa"
        app:srcCompat="@drawable/b_new_jam" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/bnj"
        app:layout_constraintEnd_toEndOf="@+id/bnj"
        app:srcCompat="@drawable/b_past" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bhb"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="7dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/bp"
        app:layout_constraintEnd_toEndOf="@+id/bp"
        app:srcCompat="@drawable/b_helpbot" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="157dp"
        android:layout_height="145dp"
        android:layout_marginTop="64dp"
        android:text="@string/welcom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.46"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</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"
        android:background="@drawable/back3"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/lb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.449"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <EditText
            android:id="@+id/editTextTextPersonName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:fontFamily="sans-serif-black"
            android:hint="@string/enter_username"
            android:importantForAutofill="no"
            android:inputType="textPersonName"
            android:textColor="#F6F6F6"
            android:textColorHint="#FFFFFF"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.262" />
    
        <EditText
            android:id="@+id/tpw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:fontFamily="sans-serif-black"
            android:hint="@string/enter_password"
            android:importantForAutofill="no"
            android:inputType="textPassword"
            android:textColor="#F6F6F6"
            android:textColorHint="#FFFDFD"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.377" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="360dp"
            android:layout_height="105dp"
            android:fontFamily="sans-serif-black"
            android:text="@string/login_sign_up"
            android:textColor="#FFFFFF"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.065" />
    
        <TextView
            android:id="@+id/reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/new_user_tap_here_to_register"
            android:textColor="#FFFFFF"
            android:textColorHint="#FFFEFE"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.494"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.852" />
    </androidx.constraintlayout.widget.ConstraintLayout>

Java布局的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:background="@drawable/back3"
    tools:context=".Bookjamroom">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bfa"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.955"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.976"
        app:srcCompat="@drawable/b_add" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bnj"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/bfa"
        app:layout_constraintEnd_toEndOf="@+id/bfa"
        app:srcCompat="@drawable/b_new_jam" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/bnj"
        app:layout_constraintEnd_toEndOf="@+id/bnj"
        app:srcCompat="@drawable/b_past" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/bhb"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="7dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:tint="@android:color/white"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/bp"
        app:layout_constraintEnd_toEndOf="@+id/bp"
        app:srcCompat="@drawable/b_helpbot" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="157dp"
        android:layout_height="145dp"
        android:layout_marginTop="64dp"
        android:text="@string/welcom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.46"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</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"
        android:background="@drawable/back3"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/lb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.449"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <EditText
            android:id="@+id/editTextTextPersonName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:fontFamily="sans-serif-black"
            android:hint="@string/enter_username"
            android:importantForAutofill="no"
            android:inputType="textPersonName"
            android:textColor="#F6F6F6"
            android:textColorHint="#FFFFFF"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.262" />
    
        <EditText
            android:id="@+id/tpw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:fontFamily="sans-serif-black"
            android:hint="@string/enter_password"
            android:importantForAutofill="no"
            android:inputType="textPassword"
            android:textColor="#F6F6F6"
            android:textColorHint="#FFFDFD"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.377" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="360dp"
            android:layout_height="105dp"
            android:fontFamily="sans-serif-black"
            android:text="@string/login_sign_up"
            android:textColor="#FFFFFF"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.065" />
    
        <TextView
            android:id="@+id/reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/new_user_tap_here_to_register"
            android:textColor="#FFFFFF"
            android:textColorHint="#FFFEFE"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.494"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.852" />
    </androidx.constraintlayout.widget.ConstraintLayout>


PS:这是我在stack overflow的第一篇文章,如果我有任何不符之处,请原谅,谢谢。类/活动也在清单中声明。动画文件没有任何问题。

您应该在onCreate方法中实例化视图

private var bfa: FloatingActionButton? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_book_jam_room)
    bfa = findViewById<FloatingActionButton>(R.id.bfa)
}

尝试对kotlin活动进行以下更改

class Bookjamroom : AppCompatActivity() {
private lateinit var bfa: FloatingActionButton 
private lateinit var bnj: FloatingActionButton 
private lateinit var bp : FloatingActionButton
private lateinit var bhb : FloatingActionButton
然后再次创建:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_book_jam_room)

        bfa = findViewById<FloatingActionButton>(R.id.bfa)
        bnj = findViewById<FloatingActionButton>(R.id.bnj)
        bp = findViewById<FloatingActionButton>(R.id.bp)
        bhb = findViewById<FloatingActionButton>(R.id.bhb)

        bfa?.setOnClickListener {
            onAddButtonClicked()
        }
        bnj?.setOnClickListener {
            Toast.makeText(this, "1button is clicked", Toast.LENGTH_SHORT).show()

        }
        bp?.setOnClickListener {
            Toast.makeText(this, "2button is clicked", Toast.LENGTH_SHORT).show()

        }
        bhb?.setOnClickListener {
            Toast.makeText(this, "3button is clicked", Toast.LENGTH_SHORT).show()

        }

    }
override-fun-onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(右布局、活动、书本、果酱、房间)
bfa=findViewById(R.id.bfa)
bnj=findViewById(R.id.bnj)
bp=findViewById(R.id.bp)
bhb=findViewById(R.id.bhb)
bfa?.setOnClickListener{
onAddButtonClicked()
}
bnj?.setOnClickListener{
Toast.makeText(单击此“1按钮”,Toast.LENGTH\u SHORT.show())
}
bp?.setOnClickListener{
Toast.makeText(点击此“2按钮”,Toast.LENGTH\u SHORT.show())
}
bhb?.setOnClickListener{
Toast.makeText(点击此“3”按钮),Toast.LENGTH\u SHORT.show()
}
}

发布的答案解决了您的问题,但只是为了解释问题-当
活动
首次初始化时(通过初始化所有
VAL
),您正在调用
findViewById
,在这种情况下,这是
活动
访问任何视图,甚至
应用程序上下文
之前的方式(这就是使它崩溃的原因)。你应该在版面膨胀后,通常是在
onCreate
中的
setContentView
之后做这些事情。我认为
lateinit var
字段对于你的视图来说是一个不错的选择-你承诺在访问它们之前设置它们,而且你不需要引入Nullability。谢谢你,这真的对我很有帮助re现在没有任何错误,它工作得很好,再次感谢!非常感谢,我现在明白了,应用程序不再崩溃,非常感谢!
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_book_jam_room)

        bfa = findViewById<FloatingActionButton>(R.id.bfa)
        bnj = findViewById<FloatingActionButton>(R.id.bnj)
        bp = findViewById<FloatingActionButton>(R.id.bp)
        bhb = findViewById<FloatingActionButton>(R.id.bhb)

        bfa?.setOnClickListener {
            onAddButtonClicked()
        }
        bnj?.setOnClickListener {
            Toast.makeText(this, "1button is clicked", Toast.LENGTH_SHORT).show()

        }
        bp?.setOnClickListener {
            Toast.makeText(this, "2button is clicked", Toast.LENGTH_SHORT).show()

        }
        bhb?.setOnClickListener {
            Toast.makeText(this, "3button is clicked", Toast.LENGTH_SHORT).show()

        }

    }