android - Kotlin 的无服务身份验证
问题描述
作为一个新手,我尽我所能在我的应用程序中制作一个非常简单的身份验证页面。
这是我的 MainActivity 类:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_auth) // I replaced activity_main by activity_auth who is made for the sign in and sign up page with a bottom-menu-bar navigation
var logUser = findViewById<EditText>(R.id.username) //username
var logPwd = findViewById<EditText>(R.id.password) //password
/*
var btnGo = findViewById<Button>(R.id.BtnLogin) // btn submit
var btnHelp = findViewById<Button>(R.id.BtnHelp) // btn pwd lost
btnGo.setOnClickListener {
val userName = logUser.text;
val passWord = logPwd.text;
Toast.makeText(this@MainActivity, "WELCOME $userName", Toast.LENGTH_LONG).show()
// the code to validate the user_name and password combination will be here
// if logUser + logPwd are good the user should be redirect to "activity_main"
setContentView(R.layout.activity_main)*/
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_profil, R.id.navigation_home))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
//}
}
就像应用程序正确显示我的登录页面一样,但是如果我删除评论标签并尝试对按钮或 ID 采取行动,应用程序就会崩溃。有没有你觉得奇怪的错误或事情?
如果您需要,这是我的登录布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#444444"
android:padding="25dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="@color/pureGold"
android:padding="30dp"
android:text="@string/login"/>
<EditText
android:id="@+id/username"
android:hint="@string/logUser"
android:textColor="@color/pureWhite"
android:textColorHint="#52afaa"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:hint="@string/logPWD"
android:textColor="@color/pureWhite"
android:textColorHint="#52afaa"
android:textAlignment="center"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="289dp"
android:layout_height="wrap_content">
<Button
android:id="@+id/BtnLogin"
android:layout_width="143dp"
android:layout_height="wrap_content"
android:textColor="@color/pureBlack"
android:text="@string/logButton1"
android:textAllCaps="false" />
<Button
android:id="@+id/BtnHelp"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:textColor="@color/pureBlack"
android:text="@string/logButton2"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是崩溃日志:
2021-03-10 20:53:33.792 8439-8439/com.example.movie_catcher E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.movie_catcher, PID: 8439
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.movie_catcher/com.example.movie_catcher.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.movie_catcher.MainActivity.onCreate(MainActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
解决方案
如果找到解决方案,但在特定片段中的 MainActivity 之外:
val v: View = inflater.inflate(R.layout.fragment_auth_sign_in, container, false)
(v.BtnLogin as Button).setOnClickListener {
var SigninUser = v.username.text
var SigninPwd = v.password.text
if (SigninUser.toString() == "Paul") {
Toast.makeText(context, "Welcome $SigninUser", Toast.LENGTH_LONG).show()
startActivity(Intent(activity, MainActivity::class.java))
} else {
Toast.makeText(context, "Unknown user : $SigninUser", Toast.LENGTH_LONG).show()
}
推荐阅读
- c# - 无法从 ComboBox 选择 C# 获取 IF 语句来更新 TextBlock
- mongodb - 我的 mongos 服务器崩溃,收到信号:11 (Segmentation fault) Mongo DB v3.4.16 on SUSE 11 SP4
- vert.x - 将vertx httpserver部署为worker verticle时如何获取等待处理的请求数?
- typescript - 如何监视 fetch/middleware 方法的调度
- html - 单个表格单元格在另一行上全宽
- php - roundcube 数据库连接失败
- reactjs - 使用 useEffect 重新渲染状态变化
- xml - 桌面模拟器 Lua 错误:“尝试对字符串值执行算术运算”
- python - 关于 np.insert 和 Fortran(90 及更高版本)等价物的问题
- javascript - 如何使用 bcrypt 从我的数据库中检索加密密码?