创建通知时出错com.example.hudixt.trigger.B.addNotification(B.java:27)

时间:2015-04-27 10:47:18

标签: java android

我正在创建一个应用程序创建通知,但我发现这样做有困难,当我尝试这样做时,我的logcat给了我错误。

04-27 16:09:27.830  13575-13575/com.example.hudixt.trigger E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
            at android.content.ComponentName.<init>(ComponentName.java:75)
            at android.content.Intent.<init>(Intent.java:3252)
            at com.example.hudixt.trigger.B.addNotification(B.java:27)
            at com.example.hudixt.trigger.B.createDialog(B.java:40)
            at com.example.hudixt.trigger.call$1.onCallStateChanged(call.java:87)
            at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:365)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)

添加通知的代码调用方法

new B().createDialog();

B.java

package com.example.hudixt.trigger;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.support.v4.app.NotificationCompat;

/**
 * Created by hudixt on 4/27/2015.
 */
public class B extends Activity {
    private void addNotification() {



        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Notifications Example")
                        .setContentText("This is a test notification");

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(2, builder.build());
    }


    public  void createDialog(){

        addNotification();

    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hudixt.trigger" >
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET" />
    <service
        android:name="service" >
    </service>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".call"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
        <activity
            android:name=".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>
    </application>

</manifest>

请帮助我,因为我是android的初学者。提前致谢

0 个答案:

没有答案