我试图访问,在允许相机类的功能,但我得到了上述错误,但当我测试允许相机代码本身它的工作正常。
package com.example.mindsync
// IntroActivity.kt
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class IntroActivity : AppCompatActivity() {
private lateinit var sharedPreferences: SharedPreferences
private lateinit var permissions: AllowingAccessCamera
private var currentScreen = 1
private val totalScreens = 2 // Set the total number of introductory screens
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE)
// Check if the app is opened for the first time
if (isFirstTime()) {
// If it's the first time, show the introductory screens
showIntroScreen()
} else {
// If not the first time, navigate to the main activity or any other desired activity
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
private fun isFirstTime(): Boolean {
return sharedPreferences.getBoolean("firstTime", true)
}
private fun showIntroScreen() {
// Set the layout for the current intro screens
when (currentScreen) {
1 -> setContentView(R.layout.getting_started)
2 -> setContentView(R.layout.allowing_access_to_camera)
}
// Set a click listener on the current screen to move to the next screen
findViewById<Button>(R.id.getting_started)?.setOnClickListener {
handleIntroButtonClick()
}
findViewById<Button>(R.id.cam_request_permission)?.setOnClickListener {
permissions = AllowingAccessCamera(this)
// Permission is not granted, request it
permissions.requestCameraPermission()
}
}
private fun handleIntroButtonClick() {
if (currentScreen < totalScreens) {
currentScreen++
showIntroScreen()
} else {
// Check if the activity is in a valid state
// Mark that the app has been opened
val editor = sharedPreferences.edit()
editor.putBoolean("firstTime", false)
editor.apply()
//Transition animation
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
// Last screen reached, navigate to the main activity
startActivity(Intent(this, MainActivity::class.java))
}
}
}
个字符
我试着在IntroActivity类中初始化它,但我一直得到空对象引用,问题是代码permissions = AllowingCamera(this)。
谢谢你
1条答案
按热度按时间fiei3ece1#
您不能/不应该以这种方式示例化Activity:
字符串
您应该在活动之间导航:
https://medium.com/swlh/navigate-between-activities-in-android-studio-c7f4617e7388
型
或者你需要重构
AllowingAccessCamera
,使其不是Activity,也不扩展AppCompatActivity
。做一些简单的事情:
型