java - 应用程序崩溃: Attempt to invoke virtual method on a null object reference

标签 java android notifications switch-statement

我正在开发一个向用户显示状态栏通知的应用程序。用户可以通过2个开关来打开和关闭通知和通知声音。我运行该应用程序并尝试打开通知,但它崩溃了。堆栈跟踪在第 60 行说(我标记了它,以便您可以轻松找到它): 尝试在空对象引用上调用虚拟方法“void android.app.NotificationManager.notify(int, android.app.Notification)”

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.RequiresApi;

import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;


public class Settings extends AppCompatActivity {
    Switch simpleswitch1;
    Switch simpleswitch2;
    private Notification notification;
    NotificationManager manager;
    Notification myNotication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);


        simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
        simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
        simpleswitch1.setChecked(false);
        simpleswitch2.setChecked(false);
        simpleswitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){{
                    Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");

                    PendingIntent pendingIntent = PendingIntent.getActivity(Settings.this, 1, intent, 0);

                    Notification.Builder builder = new Notification.Builder(Settings.this);

                    builder.setAutoCancel(false);
                    builder.setTicker("this is ticker text");
                    builder.setContentTitle("WhatsApp Notification");
                    builder.setContentText("You have a new message");
                    builder.setSmallIcon(R.drawable.notification);
                    builder.setContentIntent(pendingIntent);
                    builder.setOngoing(true);
                    builder.setSubText("This is subtext...");
                    builder.setNumber(100);
                    builder.build();

                    myNotication = builder.getNotification();
                    manager.notify(11, myNotication);//THE ERROR LED HERE
            }


        }
                simpleswitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked){
                    notification.defaults |= Notification.DEFAULT_SOUND;




                }}






    });}});}}

最佳答案

崩溃的原因是您没有初始化 manager 属性。您可以在使用 manager 之前执行此操作,如下所示:

manager = getSystemService(Context.NOTIFICATION_SERVICE)

关于java - 应用程序崩溃: Attempt to invoke virtual method on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60914106/

相关文章:

java - cassandra 大量写入有时会默默地失败

java - 尝试将 ObjectInputStream 的输出保存到文件时出现 IOException

Android 命令转换为 Swift 4 以用于 Firebase Analytics

android - 监控来自其他应用程序的通知

web-services - 来自 Web 服务器的 growl 通知

Java新手面临的基本问题

java - 正则表达式提取组中的子字符串

java.lang.IllegalArgumentException : AppCompat does not support the current theme features

android - Android 库模块的最小 build.gradle

Azure 通知中心 : what are registration types "Native" and "Template"?