英文:
App crashes whenever a Kotlin activity is launched from a Java Activity while using Intent
问题
以下是翻译好的内容:
这个应用目前有三个活动,一个登录活动,一个注册活动,以及一个用 Kotlin 编写的名为 Bookjamroom 的活动。Bookjamroom 活动应该在点击登录按钮时显示,但每当我这样做时,应用就会强制关闭。
Log Cat 错误:
- 2020-10-15 20:17:53.047 12954-12981/com.example.jamsecure E/eglCodecCommon: glUtilsParamSize: 未知参数 0x000082da
- 2020-10-15 20:17:53.303 12954-12981/com.example.jamsecure D/EGL_emulation: eglMakeCurrent: 0xebb840c0: 版本 3 1 (tinfo 0xebb832e0)
- 2020-10-15 20:18:03.883 12954-12954/com.example.jamsecure D/AndroidRuntime: 关闭虚拟机 2020-10-15 20:18:03.889
- 12954-12954/com.example.jamsecure E/AndroidRuntime: 致命异常:
- 主进程:com.example.jamsecure,PID:12954
- java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com.example.jamsecure/com.example.jamsecure.Bookjamroom}:java.lang.NullPointerException: 尝试调用虚拟方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' 但对象引用为空
- 引起的原因:java.lang.NullPointerException:尝试调用虚拟方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' 但对象引用为空
- ...(以下为详细堆栈跟踪)
Kotlin .kt 类代码
package com.example.jamsecure
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class Bookjamroom : AppCompatActivity() {
// 这里是类的定义,包括变量和函数,省略了具体内容
}
调用 Kotlin 类的 Java 代码
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// 这里是类的定义,包括变量和函数,省略了具体内容
}
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">
<!-- 这里是布局的定义,包括视图和约束,省略了具体内容 -->
</androidx.constraintlayout.widget.ConstraintLayout>
Java 布局文件
<?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">
<!-- 这里是布局的定义,包括视图和约束,省略了具体内容 -->
</androidx.constraintlayout.widget.ConstraintLayout>
希望这些翻译能够帮助你理解你提供的代码和信息。如果你需要进一步的帮助,请随时提问。
英文:
This app currently has three activities, a Login activity, Register activity and an activity written in Kotlin called the Bookjamroom activity. The Bookjamroom activity is supposed to be displayed On click of the login button, but whenever i do so the app forcefully shuts down.
Log Cat Error:
- 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 .kt class code
package com.example.jamsecure
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class Bookjamroom : AppCompatActivity() {
private val bfa = findViewById<FloatingActionButton>(R.id.bfa)
private val bnj = findViewById<FloatingActionButton>(R.id.bnj)
private val bp = findViewById<FloatingActionButton>(R.id.bp)
private val bhb = findViewById<FloatingActionButton>(R.id.bhb)
private val rotateOpen: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.rotate_open_anim) }
private val rotateClose: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.rotate_close_anim) }
private val fromBottom: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.from_bottom) }
private val toBottom: Animation by lazy { AnimationUtils.loadAnimation(this, R.anim.to_bottom) }
private var clicked = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_book_jam_room)
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()
}
}
private fun onAddButtonClicked() {
setVisibility(clicked)
setAnimation(clicked)
clicked = !clicked
}
private fun setVisibility(clicked: Boolean) {
if (!clicked) {
bnj.visibility = View.VISIBLE
bp.visibility = View.VISIBLE
bhb.visibility = View.VISIBLE
} else {
bnj.visibility = View.INVISIBLE
bp.visibility = View.INVISIBLE
bhb.visibility = View.INVISIBLE
}
}
private fun setAnimation(clicked: Boolean) {
if (!clicked) {
bnj.startAnimation(fromBottom)
bp.startAnimation(fromBottom)
bhb.startAnimation(fromBottom)
bfa.startAnimation(rotateOpen)
} else {
bnj.startAnimation(toBottom)
bp.startAnimation(toBottom)
bhb.startAnimation(toBottom)
bfa.startAnimation(rotateClose)
}
}
}
Java Code that calls the Kotlin class
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.reg);
Button lbtn = findViewById(R.id.lb);
lbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Bookjamroom.class);
startActivity(i);
}
});
SpannableString spannableString = new SpannableString("New user? tap here to register!");
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(@NotNull View v) {
Intent intent = new Intent(MainActivity.this, Register.class);
startActivity(intent);
}
};
spannableString.setSpan(span, 0, 31, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
XML file of Kotlin Activity's Layout
<?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 of Java Layout
<?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: This is my first post here at stack overflow, please excuse me if I have made any discrepancies, Thank you. Also the classes/activities are declared in the Manifest. And there isn't any issue with the Anim files.
答案1
得分: 0
你应该在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)
}
或者可以直接通过id访问:
import kotlinx.android.synthetic.main.activity_book_jam_room.*
class Bookjamroom : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_book_jam_room)
bfa?.setOnClickListener {
onAddButtonClicked()
}
}
}
英文:
You should instantiate views in onCreate method.
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)
}
or use access directly by id:
import kotlinx.android.synthetic.main.activity_book_jam_room.*
class Bookjamroom : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_book_jam_room)
bfa?.setOnClickListener {
onAddButtonClicked()
}
}
答案2
得分: 0
在你的 Kotlin 活动中尝试以下更改:
class Bookjamroom : AppCompatActivity() {
private lateinit var bfa: FloatingActionButton
private lateinit var bnj: FloatingActionButton
private lateinit var bp : FloatingActionButton
private lateinit var bhb : FloatingActionButton
然后在 onCreate
方法中:
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()
}
}
英文:
Try the following change on your kotlin activity.
class Bookjamroom : AppCompatActivity() {
private lateinit var bfa: FloatingActionButton
private lateinit var bnj: FloatingActionButton
private lateinit var bp : FloatingActionButton
private lateinit var bhb : FloatingActionButton
then onCreate:
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()
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论