我想制作一个小型通知程序,我收到此错误 请求任何人可以帮助我为我的项目需要它
11-19 23:42:40.297 5971-5971/com.example.codeiv.ewtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.codeiv.ewtest, PID: 5971
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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.codeiv.ewtest/0x1090085 vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE)
at android.app.NotificationManager.notify(NotificationManager.java:222)
at android.app.NotificationManager.notify(NotificationManager.java:194)
at com.example.codeiv.ewtest.main_menu.test1(main_menu.java:54)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
11-19 23:42:42.658 5971-5971 / com.example.codeiv.ewtest I / Process:发送信号。 PID:5971 SIG:9
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
}
public void inf(View view) {
Intent intent = new Intent(this , nfo.class);
startActivity(intent);
}
public void test1(View view){
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, nfo.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(nfo.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
mId++;
}
答案 0 :(得分:0)
为通知设置一个图标(您可以使用android studio默认图标进行测试),并确保您的按钮onClick方法名称,即test1()与您在按钮的属性属性中设置的名称相同。
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("My notification")
.setContentText("Hello World!");
.setSmallIcon(R.drawable.cast_ic_notification_0)