创建 Intent 结果 "Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()'"

Creating Intent results in "Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()'"

我正在尝试按照 Google 上的代码进行 creating a notification on Wear OS devices

import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.NotificationCompat.WearableExtender

class MainActivity : WearableActivity() {


    val notificationId = 1
    // The channel ID of the notification.
    val id = "my_channel_01"
    // Build intent for notification content
    val viewPendingIntent = Intent(this, com.alborz.innout.MainActivity::class.java).let { viewIntent ->
        viewIntent.putExtra(EXTRA_EVENT_ID, notificationId)
        PendingIntent.getActivity(this, 0, viewIntent, 0)
    }
    // Notification channel ID is ignored for Android 7.1.1
    // (API level 25) and lower.
    val notificationBuilder = NotificationCompat.Builder(this, id)
        .setLocalOnly(true)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("TITLE")
        .setContentText("TEXT")
        .setContentIntent(viewPendingIntent)

稍后我尝试激活通知,如下所示:

    button_in.setOnClickListener {
        NotificationManagerCompat.from(this).apply {
            notify(notificationId, notificationBuilder.build())
        }
    }

但是我收到一个我不明白的错误。 (我是 Kotlin 的超级新手,所以可能错过了一些明显的东西)

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.alb.innout/com.alb.innout.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2844)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6680)
        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.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ContextWrapper.getPackageName(ContextWrapper.java:142)
        at android.content.ComponentName.<init>(ComponentName.java:130)
        at android.content.Intent.<init>(Intent.java:6082)
        at com.alb.innout.MainActivity.<init>(MainActivity.kt:46)

第 46 行是 val viewPendingIntent = Intent ...

您应该将此 Intent 和通知设置放在一个函数中,并在发送通知时调用该函数。按照你现在的方式,Intent 是在实例化 Activity 时创建的,这是在 onCreate() 之前,而且对于它作为函数的 this 参数传递来说还为时过早和想要使用其上下文的构造函数。