0

when click button on 'listview', I want to use Push Notification. But there is Error..

'ListViewAdapter'

 public class ListViewAdapter extends BaseAdapter{
private Context con;
public ListViewAdapter(Context con) {
        this.con = con;
        getData("http://117.17.158.240/test/GpsSender.php");
    }
....
... 
 @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final int pos = position;
        final Context context = parent.getContext();

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(com.satt.mjbus.R.layout.listview_item, parent, false);

        }
 final ImageButton btn = (ImageButton)convertView.findViewById(com.satt.mjbus.R.id.button);
        btn.setTag(position);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int poss = (Integer) v.getTag();
                int check_num = 0;

                btn.setSelected(true);
                if (check_num == 0) {
                    btn.setSelected(true);
                    check_num = 1;
                } else if (check_num == 1) {
                    btn.setSelected(false);
                    check_num = 0;
                }
                NotificationManager notificationManager = (NotificationManager) con.getSystemService(con.NOTIFICATION_SERVICE);
                Intent intent1 = new Intent(con.getApplicationContext(), ListViewAdapter.class); 

                Notification.Builder builder = new Notification.Builder(con.getApplicationContext());
                intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

                PendingIntent pendingNotificationIntent = PendingIntent.getActivity(con, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

                builder.setSmallIcon(R.drawable.bus)
                        .setTicker("HETT")
                        .setWhen(System.currentTimeMillis())
                        .setNumber(1)
                        .setContentTitle("푸쉬 제목")
                        .setContentText("푸쉬내용")
                        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                        .setContentIntent(pendingNotificationIntent)
                        .setAutoCancel(true)
                        .setOngoing(true);

                notificationManager.notify(1, builder.build());

            }

        });
        btn.setFocusable(false);

....
}

And I got error is java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

plz help..

2
  • Your context is null. Put the code of the activity or fragment when you're calling ListViewAdapter please Commented Jun 6, 2017 at 8:55
  • replace context with this Commented Jun 6, 2017 at 8:55

2 Answers 2

2

Replace this line of code

LayoutInflater inflater = 
    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

with

LayoutInflater inflater = 
    (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Sign up to request clarification or add additional context in comments.

3 Comments

it's not work.. Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)'
remove this line too final Context context = parent.getContext();
i got it. thk you
0

replace LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); with

LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.