我有下一个代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
为什么它不起作用?它什么也没显示。在 Android 2.2.1 上测试。
更新。活动代码:
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void start(View view) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
}
}
布局中的按钮:
<Button
android:id="@+id/startButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="@+id/aboutButton"
android:layout_alignLeft="@+id/aboutButton"
android:layout_marginBottom="20dp"
android:onClick="start"
android:text="Start" />
更新2。错误日志:
02-04 11:40:40.752: D/dalvikvm(23054): GC_EXTERNAL_ALLOC freed 859 objects / 59168 bytes in 61ms
02-04 11:40:56.424: D/AndroidRuntime(23054): Shutting down VM
02-04 11:40:56.424: W/dalvikvm(23054): threadid=1: thread exiting with uncaught exception (group=0x4001d888)
02-04 11:40:56.432: E/AndroidRuntime(23054): FATAL EXCEPTION: main
02-04 11:40:56.432: E/AndroidRuntime(23054): java.lang.IllegalStateException: Could not execute method of the activity
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$1.onClick(View.java:2082)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View.performClick(View.java:2461)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$PerformClick.run(View.java:8890)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Handler.handleCallback(Handler.java:587)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Looper.loop(Looper.java:123)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.ActivityThread.main(ActivityThread.java:4632)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 11:40:56.432: E/AndroidRuntime(23054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 11:40:56.432: E/AndroidRuntime(23054): at dalvik.system.NativeStart.main(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.reflect.InvocationTargetException
02-04 11:40:56.432: E/AndroidRuntime(23054): at pckg.mywebsites.LoginActivity.start(LoginActivity.java:28)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$1.onClick(View.java:2077)
02-04 11:40:56.432: E/AndroidRuntime(23054): ... 11 more
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=pckg.mywebsites id=222 notification=Notification(vibrate=null,sound=null,defaults=0x0)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Parcel.readException(Parcel.java:1264)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Parcel.readException(Parcel.java:1248)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.NotificationManager.notify(NotificationManager.java:110)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.NotificationManager.notify(NotificationManager.java:90)
02-04 11:40:56.432: E/AndroidRuntime(23054): ... 15 more