【问题标题】:android.content.Context.getSystemService(java.lang.String)' on a null object referenceandroid.content.Context.getSystemService(java.lang.String)' 在空对象引用上
【发布时间】:2017-06-06 08:52:45
【问题描述】:

当单击“listview”上的按钮时,我想使用推送通知。 但是有错误..

'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);

....
}

我得到的错误是 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”

请帮忙..

【问题讨论】:

  • 您的上下文为空。请在调用 ListViewAdapter 时输入活动或片段的代码
  • 用这个替换上下文

标签: java android listview notifications


【解决方案1】:

替换这行代码

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

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

【讨论】:

  • 不行。尝试调用虚方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'
  • 也去掉这行final Context context = parent.getContext();
  • 我明白了。谢谢你
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 2016-07-29
    相关资源
    最近更新 更多