2016-11-19 98 views
0
package com.example.khatrimann.notification; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent intent = new Intent(this, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    NotificationCompat.Builder b = new NotificationCompat.Builder(this); 

    b.setTicker("khatrimann") 
      .setContentTitle("Default notification") 
      .setContentText("Hey There.") 
      .setContentIntent(contentIntent); 


    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, b.build()); 
} 
} 

此代码在棉花糖上测试,它正在崩溃并且无法正常工作。通知活动崩溃

android.support.v4和v7,都尝试一个接一个,然后应用程序崩溃。

的logcat如下:显示

11-20 18:24:59.623 4146-4146/com.example.khatrimann.notification E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.khatrimann.notification, PID: 4146 
                       Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system} 
                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.khatrimann.notification/com.example.khatrimann.notification.MainActivity}: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=com.example.khatrimann.notification/0x1090085 vibrate=null sound=null tick defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5461) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                       Caused by: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=com.example.khatrimann.notification/0x1090085 vibrate=null sound=null tick defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE) 
                        at android.app.NotificationManager.notify(NotificationManager.java:223) 
                        at android.app.NotificationManager.notify(NotificationManager.java:195) 
                        at com.example.khatrimann.notification.MainActivity.onCreate(MainActivity.java:32) 
                        at android.app.Activity.performCreate(Activity.java:6251) 
                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)  
                        at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:148)  
                        at android.app.ActivityThread.main(ActivityThread.java:5461)  
                        at java.lang.reflect.Method.invoke(Native Method)  
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

错误是致命的主要

+0

在这里发布logcat。 –

回答

4

当你创建你必须添加一个小图标,像这样的代码示例的通知。

NotificationCompat.Builder mBuilder = 
    new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.notification_icon) 
    .setContentTitle("My notification") 
    .setContentText("Hello World!"); 
+0

非常感谢!它帮助 – Mann

+0

你能接受答案吗? –

+0

是啊!在工作室试过,它工作。错误地在文档中,我跳过了这部分。 PS:由于我没有足够的代表,因此无法投票。 – Mann