在android中从JSON Array发送多个通知

时间:2014-06-17 21:11:42

标签: android json

我正在处理可以从JSON数组发送多个通知的内容。 这是我的错误导致它我想:

    06-17 23:07:14.108: E/AndroidRuntime(20644): Caused by: java.lang.NullPointerException
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:94)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:165)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:151)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at eu.vanmelzen.mientoentje.Notifications.createNotification(Notifications.java:59)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at eu.vanmelzen.mientoentje.Notifications$NotificationManager.doInBackground(Notifications.java:108)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at eu.vanmelzen.mientoentje.Notifications$NotificationManager.doInBackground(Notifications.java:1)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    06-17 23:07:14.108: E/AndroidRuntime(20644):    ... 4 more

这是它引用的一段代码:

    protected String doInBackground(String... args) {

            json = jParser.getJSONFromUrl(NOTIFICATIES_URL);
            try
            {
                mNotificaties = json.getJSONArray(TAG_NOTIFICATIES);
                // looping through all posts according to the json object returned
                for (int i = 0; i < mNotificaties.length(); i++) 
                {
                    JSONObject c = mNotificaties.getJSONObject(i);

                    // gets the content of each tag
                    id = Integer.parseInt(c.getString(TAG_ID));
                    message = c.getString(TAG_MSG);
                    title = c.getString(TAG_TITLE);
                    delete = c.getString(TAG_DELETE);

                    createNotification(message, title, id);
                }
            }
            catch(JSONException e)
            {

            }
            return null;

开始这种方法:

    public void createNotification(String Message, String Title, int NotificationId)
    {
        //Notificatie aanmaken
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(Title)
            .setContentText(Message);

        Intent resultIntent = new Intent(this, Login.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(Login.class);

        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

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

        // Builds the notification and issues it.
        mNotificationManager.notify();
    }

请有人帮助我,我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我已将for循环放在另一个方法中,这是我的创建通知方法:

    public void createNotification(String Message, String Title,
        int NotificationId) {

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(Title).setContentText(Message);
    // Creates an explicit intent for an Activity in your app

    int mNotificationId = NotificationId;

    Intent resultIntent = new Intent(this, Kas.class);

    // The stack builder object will contain an artificial back stack for
    // the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(HomeScreen.class);

    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    edit.putString("kasnaam", title);
    edit.commit();
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mNotificationId, mBuilder.build());
}