我开始使用视频教程,其中一个问题是当您单击启动新活动的按钮时,模拟器和手机中的程序崩溃。课程本身在这里:https ://neco-desarrollo.es/2021/04/%D1%83%D1%80%D0%BE%D0%BA-18-%D0%BF%D1%80%D0% B0% D0%BA%D1%82%D0%B8%D0%BA%D0%B0-%D0%BA%D0%BE%D0%B4-%D1%83%D1%80%D0%BE%D0% BA% D0%B0
我必须马上说,我阅读了视频上的所有评论,出现了这样的问题,但没有人写出解决方案。
我正在尝试运行的活动代码:
package com.example.practic
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.practic.constance.Constance
import com.example.practic.databinding.ActivitySignInUpBinding
class SignInUpActivity : AppCompatActivity() {
lateinit var bindingClass: ActivitySignInUpBinding
var signState = "empty"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bindingClass = ActivitySignInUpBinding.inflate(layoutInflater)
setContentView(bindingClass.root)
signState = intent.getStringExtra(Constance.SIGN_STATE)!!
if (signState == Constance.SIGN_IN_STATE){
bindingClass.edName.visibility = View.GONE
bindingClass.edName2.visibility = View.GONE
bindingClass.edName3.visibility = View.GONE
bindingClass.imAvatar.visibility = View.INVISIBLE
}
}
fun onClickDone(view: View){
if (signState == Constance.SIGN_IN_STATE){
}
}
fun onClickAvatar(view: View){
bindingClass.imAvatar.visibility = View.VISIBLE
}
}
我启动的活动代码:
package com.example.practic
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.practic.constance.Constance
import com.example.practic.databinding.ActivityMainBinding
import com.example.practic.databinding.ActivitySignInUpBinding
class MainActivity : AppCompatActivity() {
lateinit var bindingClass : ActivityMainBinding
private var login: String = "empty"
private var password: String = "empty"
private var name: String = "empty"
private var name2: String = "empty"
private var name3: String = "empty"
private var avatarImId: Int = 0
override fun onCreate(s: Bundle?) {
super.onCreate(s)
bindingClass = ActivityMainBinding.inflate(layoutInflater)
setContentView(bindingClass.root)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == Constance.REQUEST_CODE_SIGN_IN){
}
else if (requestCode == Constance.REQUEST_CODE_SIGN_UP){
}
}
fun onClickSignIn (view: View){
val intent = Intent(this,ActivitySignInUpBinding ::class.java)
intent.putExtra(Constance.SIGN_STATE, Constance.REQUEST_CODE_SIGN_IN)
startActivityForResult(intent, Constance.REQUEST_CODE_SIGN_IN)
}
fun onClickSignUp (view: View){
val intent = Intent(this,ActivitySignInUpBinding ::class.java)
intent.putExtra(Constance.SIGN_STATE, Constance.REQUEST_CODE_SIGN_UP)
startActivityForResult(intent, Constance.REQUEST_CODE_SIGN_UP)
}
}
在清单中,新活动添加了:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practic">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Practic">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignInUpActivity"
android:exported="true">
</activity>
</application>
</manifest>
它在 logcat 中给出以下错误:
2022-05-10 00:07:09.044 3574-3600/com.example.practic E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2022-05-10 00:07:15.681 3574-3574/com.example.practic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.practic, PID: 3574
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.practic/com.example.practic.databinding.ActivitySignInUpBinding}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2005)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4586)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:597)
at android.app.Activity.startActivityForResult(Activity.java:4544)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:583)
at com.example.practic.MainActivity.onClickSignIn(MainActivity.kt:39)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2022-05-10 00:07:31.148 3657-3687/com.example.practic E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
可能是什么原因以及如何解决?
我在清单中添加了一个活动,修复了点击监听器,但现在出现了以下错误:
Caused by: java.lang.NullPointerException
at com.example.practic.SignInUpActivity.onCreate(SignInUpActivity.kt:19)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
问题出在行 signState = intent.getStringExtra(Constance.SIGN_STATE)!! 当它被删除时,活动开始,但没有必要的编辑。