我正在尝试使警报在关闭应用程序时发送通知。我在MainActivity中创建了一个警报管理器,并在触发onReceive()
后立即为BroadcastReceiver创建了一个内部类,我需要发送通知。警报有效,但是每次尝试发送通知时,都会出现[]]错误
2020-03-25 13:19:33.166 15045-15045 / com.kotlin.ambulantlcsE / AndroidRuntime:致命异常:main流程:com.kotlin.ambulantlcs,PID:15045java.lang.RuntimeException:无法启动接收器com.kotlin.ambulantlcs.ui.MainActivity $ Receiver:kotlin.UninitializedPropertyAccessException:lateinit属性notificationManager尚未初始化在android.app.ActivityThread.handleReceiver(ActivityThread.java:3997)在android.app.ActivityThread.access $ 1500(ActivityThread.java:267)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1992)在android.os.Handler.dispatchMessage(Handler.java:107)在android.os.Looper.loop(Looper.java:237)在android.app.ActivityThread.main(ActivityThread.java:7777)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)原因:kotlin.UninitializedPropertyAccessException:lateinit属性notificationManager尚未初始化在com.kotlin.ambulantlcs.ui.MainActivity $ Receiver.onReceive(MainActivity.kt:100)在android.app.ActivityThread.handleReceiver(ActivityThread.java:3988)在android.app.ActivityThread.access $ 1500(ActivityThread.java:267)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1992)在android.os.Handler.dispatchMessage(Handler.java:107)在android.os.Looper.loop(Looper.java:237)在android.app.ActivityThread.main(ActivityThread.java:7777)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
请在下面查看我的完整代码。
import android.app.* import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.graphics.Color import android.os.Build import android.os.Bundle import android.util.Log import android.widget.RemoteViews import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.navigation.NavOptions import androidx.navigation.Navigation import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.NavigationUI import com.kotlin.ambulantlcs.R import com.kotlin.ambulantlcs.storage.SharedDataManager import com.kotlin.ambulantlcs.ui.fragments.LoginFragmentDirections import java.util.* open class MainActivity : AppCompatActivity() { lateinit var context: Context lateinit var alarmManager: AlarmManager val obj: MainActivity = this override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val appBarConfiguration = AppBarConfiguration .Builder(R.id.homeFragment, R.id.loginFragment) .build() setAlarm() if(SharedDataManager.getInstance(this).isLoggedIn) { var navOptions = NavOptions.Builder() .setPopUpTo(R.id.action_login_Home, true) .build() val navController = Navigation.findNavController(this, R.id.fragment) NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration) navController.navigate( LoginFragmentDirections.actionLoginHome() ) } else { val navController = Navigation.findNavController(this, R.id.fragment ) NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration) } } override fun onBackPressed() { Toast.makeText(applicationContext, "Function is not allowed", Toast.LENGTH_SHORT).show() } override fun onSupportNavigateUp(): Boolean { return NavigationUI.navigateUp(Navigation.findNavController(this, R.id.fragment), null) } fun setAlarm() { Log.d("MainActivity", "Create: ${Date().toString()}") context = this alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val seconds = 10 * 1000 val intent = Intent(context, Receiver::class.java) val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) Log.d("MainActivity", "SECONDS: ${seconds.toLong()}") alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, seconds.toLong(), seconds.toLong(), pendingIntent) } class Receiver : BroadcastReceiver() { lateinit var notificationManager : NotificationManager lateinit var notificationChannel : NotificationChannel lateinit var builder : Notification.Builder private val channelId = "i.apps.notifications" private val description = "Test notification" override fun onReceive(context: Context?, intent: Intent?) { Log.d("MainActivity", " Receiver: ${Date().toString()}") val intent = Intent(context, MainActivity::class.java) val pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) val contentView = RemoteViews("com.kotlin.ambulantlcs", R.layout.activity_main) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationChannel = NotificationChannel( channelId,description,NotificationManager.IMPORTANCE_HIGH) notificationChannel.enableLights(true) notificationChannel.lightColor = Color.GREEN notificationChannel.enableVibration(false) notificationManager.createNotificationChannel(notificationChannel) builder = Notification.Builder(context, channelId) .setContent(contentView) .setSmallIcon(R.drawable.ic_launcher_background) .setContentIntent(pIntent) }else{ builder = Notification.Builder(context) .setContent(contentView) .setSmallIcon(R.drawable.ic_launcher_background) .setContentIntent(pIntent) } notificationManager.notify(1234,builder.build()) } } }
您能帮我解决这个问题吗?谢谢。
我正在尝试使警报在关闭应用程序时发送通知。我在MainActivity中创建了一个警报管理器,并在...
您的notificationManger为空(您不初始化他)。试试这个:
应用程序崩溃的原因是您没有初始化notificationManager
。除了将其声明为lateinit var
之外,您还可以在Receiver
类中将其初始化: