如何解决每当使用Intent
此应用程序当前具有三个活动,一个是登录活动,一个注册活动以及一个用科特林编写的活动,称为Bookjamroom活动。应该在单击登录按钮时显示Bookjamroom活动,但是每当我这样做时,该应用程序都会强制关闭。
记录目录错误:
- 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类代码
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)
}
}
}
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,31,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
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>
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=".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:这是我在堆栈溢出时的第一个帖子,如果我有任何出入之处,请原谅,谢谢。类别/活动也在清单中声明。 Anim文件没有任何问题。
解决方法
您应该在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()
}
}
,
尝试对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()
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。