NullPointerException在NotificationCompat.Builder中的通知

威利·铂(Willy Pt)

我试图建立通知,但似乎NotificationCompat.Builder NPE在这一行是我的mNotificationManager.notify(NOTIFICATION_ID, t);

经过10个小时的研究,我解决了许多问题,包括确保我Intent作为服务运行,上下文能够正确解析以及onCreate运行一次。

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.willypt.himtichannelsubscribe"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.willypt.himtichannelsubscribe.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.willypt.himtichannelsubscribe.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.willypt.himtichannelsubscribe.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.willypt.himtichannelsubscribe.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.willypt.himtichannelsubscribe" />
            </intent-filter>
        </receiver>

        <service android:name="com.willypt.himtichannelsubscribe.GcmIntentService" />

        <activity
            android:name="com.willypt.himtichannelsubscribe.ChannelActivity"
            android:label="@string/title_activity_channel" >
        </activity>
    </application>

</manifest>

ParseJSON.java,我的readLink()函数是调用GcmIntentService的函数

package com.willypt.himtichannelsubscribe;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.willypt.himtichannelsubscribe.GcmIntentService;

public class ParseJSON extends Activity {

/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.
    ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
  }
  public ArrayList<String> GetJSON(String url){
        ArrayList<String> val = new ArrayList<String>();
        String readLink = readLink(url);
        JSONArray jsonArray = new JSONArray();
        try {
          jsonArray = new JSONArray(readLink);
          for (int i = 0; i < jsonArray.length(); i++) {
              val.add(jsonArray.getString(i));
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        return val;
  }
  public String readLink(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    Log.e(ParseJSON.class.toString(),url);
    try {
      HttpResponse response = client.execute(httpGet);
      StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String line;
        while ((line = reader.readLine()) != null) {
          builder.append(line);
        }
      } else {
        Log.e(ParseJSON.class.toString(), "Failed to download file");

        Intent Notif = new Intent(this, GcmIntentService.class);
        Notif.putExtra("PostThisNotifString", "Failed to download file");
        this.startService(Notif);
      }
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return builder.toString();
  }
}

GcmIntentService.java

package com.willypt.himtichannelsubscribe;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    private PendingIntent contentIntent;

    public GcmIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "HIMTI Notification v2";
    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM will be
             * extended in the future with new message types, just ignore any message types you're
             * not interested in, or that you don't recognize.
             */
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " + extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            } else {
                //I just need to post a simple notification to user
                String ex = intent.getStringExtra("PostThisNotifString");
                sendNotification(ex);
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    public void sendNotification(String msg) {
        Intent u = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, u,0);


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setContentTitle("HIMTI Broadcast")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);
        Notification t = mBuilder.build();
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, t);
    }
}

这是logcat

10-13 11:45:36.429: E/AndroidRuntime(16096): FATAL EXCEPTION: IntentService[GcmIntentService]
10-13 11:45:36.429: E/AndroidRuntime(16096): java.lang.NullPointerException
10-13 11:45:36.429: E/AndroidRuntime(16096):    at com.willypt.himtichannelsubscribe.GcmIntentService.sendNotification(GcmIntentService.java:108)
10-13 11:45:36.429: E/AndroidRuntime(16096):    at com.willypt.himtichannelsubscribe.GcmIntentService.onHandleIntent(GcmIntentService.java:85)
10-13 11:45:36.429: E/AndroidRuntime(16096):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-13 11:45:36.429: E/AndroidRuntime(16096):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 11:45:36.429: E/AndroidRuntime(16096):    at android.os.Looper.loop(Looper.java:137)
10-13 11:45:36.429: E/AndroidRuntime(16096):    at android.os.HandlerThread.run(HandlerThread.java:60)

我的猜测:您尚未初始化mNotificationManager字段。

您可能需要在您的产品中添加以下内容onHandleIntent()

if (mNotificationManager == null) {
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

NullPointerException在NotificationCompat.Builder中的通知

来自分类Dev

NotificationCompat Builder显示通知问题

来自分类常见问题

单击通知后实施Intent方法(NotificationCompat.Builder)

来自分类Dev

使用NotificationCompat.Builder构建GCM通知的图标问题

来自分类常见问题

Android O中弃用了NotificationCompat.Builder

来自分类Dev

NotificationCompat.Builder中的setTimeoutAfter不起作用

来自分类Dev

Android:如何在通知范围内调整NotificationCompat.Builder的largeIcon

来自分类Dev

NotificationCompat.Builder是否需要setContentIntent(PendingIntent)?

来自分类Dev

NotificationCompat.Builder是否需要setContentIntent(PendingIntent)?

来自分类Dev

日食中没有NotificationCompat

来自分类Dev

NotificationCompat在Koitlin中遇到的困难

来自分类Dev

NotificationCompat中的BigTextStyle导致图形错误

来自分类Dev

Android NotificationCompat未显示在jar中

来自分类Dev

如何将android通知转换为NotificationCompat

来自分类Dev

Fragment类中AlertDialog.Builder(getActivity())上的NullPointerException

来自分类Dev

对于类型NotificationCompat.Builder,未定义方法setColor(int)。

来自分类Dev

如何在Android Studio中导入android.support.v7.app.NotificationCompat.Builder类

来自分类Dev

适用于android.support.v4.app.NotificationCompat $ Builder的ClassNotFoundException

来自分类Dev

是否必须使用NotificationCompat和NotificationManagerCompat通知才能出现在可穿戴设备上?

来自分类Dev

带电容器的离子性本地通知:找不到NotificationCompat

来自分类Dev

使用NotificationCompat.MediaStyle和MediaSessionCompat从通知控制我的网络广播播放器

来自分类Dev

在Service中发送通知时,为什么build()中存在NullPointerException?

来自分类Dev

我在Android中收到nullPointerException。我正在通过通知实现texttospeech功能

来自分类Dev

Android 广播通知 NullPointerException on Intent Extra

来自分类Dev

片段中OnClickListener的NullPointerException

来自分类Dev

Grails中的NullPointerException错误

来自分类Dev

NullPointerException:DialogAlert中的EditText

来自分类Dev

在Java中避免NullPointerException

来自分类Dev

线程中的NullPointerException

Related 相关文章

  1. 1

    NullPointerException在NotificationCompat.Builder中的通知

  2. 2

    NotificationCompat Builder显示通知问题

  3. 3

    单击通知后实施Intent方法(NotificationCompat.Builder)

  4. 4

    使用NotificationCompat.Builder构建GCM通知的图标问题

  5. 5

    Android O中弃用了NotificationCompat.Builder

  6. 6

    NotificationCompat.Builder中的setTimeoutAfter不起作用

  7. 7

    Android:如何在通知范围内调整NotificationCompat.Builder的largeIcon

  8. 8

    NotificationCompat.Builder是否需要setContentIntent(PendingIntent)?

  9. 9

    NotificationCompat.Builder是否需要setContentIntent(PendingIntent)?

  10. 10

    日食中没有NotificationCompat

  11. 11

    NotificationCompat在Koitlin中遇到的困难

  12. 12

    NotificationCompat中的BigTextStyle导致图形错误

  13. 13

    Android NotificationCompat未显示在jar中

  14. 14

    如何将android通知转换为NotificationCompat

  15. 15

    Fragment类中AlertDialog.Builder(getActivity())上的NullPointerException

  16. 16

    对于类型NotificationCompat.Builder,未定义方法setColor(int)。

  17. 17

    如何在Android Studio中导入android.support.v7.app.NotificationCompat.Builder类

  18. 18

    适用于android.support.v4.app.NotificationCompat $ Builder的ClassNotFoundException

  19. 19

    是否必须使用NotificationCompat和NotificationManagerCompat通知才能出现在可穿戴设备上?

  20. 20

    带电容器的离子性本地通知:找不到NotificationCompat

  21. 21

    使用NotificationCompat.MediaStyle和MediaSessionCompat从通知控制我的网络广播播放器

  22. 22

    在Service中发送通知时,为什么build()中存在NullPointerException?

  23. 23

    我在Android中收到nullPointerException。我正在通过通知实现texttospeech功能

  24. 24

    Android 广播通知 NullPointerException on Intent Extra

  25. 25

    片段中OnClickListener的NullPointerException

  26. 26

    Grails中的NullPointerException错误

  27. 27

    NullPointerException:DialogAlert中的EditText

  28. 28

    在Java中避免NullPointerException

  29. 29

    线程中的NullPointerException

热门标签

归档