마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

service getContentResolver()' on a null object reference 이오류가 자꾸뜨네요ㅠ

0 추천

메인페이지에서 버튼클릭할시

RealService 클래스 안에 public void sendNotification(Double Lat, Double Long) 를 호출시켜

푸쉬를 전송하려고하는데 계속 오류가나네요..

 Process: dpm.location.tracker, PID: 20887
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.app.PendingIntent.getActivity(PendingIntent.java:345)
        at android.app.PendingIntent.getActivity(PendingIntent.java:312)
        at dpm.location.tracker.RealService.sendNotification(RealService.java:152)
        at dpm.location.tracker.JobServiceDemoActivity$1.onClick(JobServiceDemoActivity.java:91)

 

 

메인페이지

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                RealService realService = new RealService();
                Double aa = 123.123;
                Double bb = 123.123;
                //realService.new update_gps2(aa, bb).execute();
                realService.sendNotification(aa,bb);

            }
        });

이구 RealService페이지는

public void sendNotification(Double Lat, Double Long) {

        Intent intent = new Intent(RealService.this, JobServiceDemoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(RealService.this.context, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); // 오류난부분입니다.


        String channelId = "fcm_default_channel";//getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher)//drawable.splash)
                        .setContentTitle("Service test")
                        .setContentText(Lat+"-"+Long)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,"Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        new update_gps2(Lat, Long).execute();
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        //notificationManager.cancelAll();
    }

입니다,

도움좀 부탁드립니다.

 

kth8295 (290 포인트) 님이 2019년 12월 11일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...