Android 关于重大通知
我已经在大通知上写了这个程序。当我运行它时,它会给出结果,但消息为java.lang.IllegalStateException:cannotexecutemethodforAndroid:onClick。先谢谢你。拜托,谁能帮我一下吗。节目如下Android 关于重大通知,android,Android,我已经在大通知上写了这个程序。当我运行它时,它会给出结果,但消息为java.lang.IllegalStateException:cannotexecutemethodforAndroid:onClick。先谢谢你。拜托,谁能帮我一下吗。节目如下 java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_main);
Button sNotification = (Button) findViewById(R.id.sNotification);
Button bigNotification = (Button) findViewById(R.id.bigNotification);
sNotification.setOnClickListener(this);
bigNotification.setOnClickListener(this);
}
private void displayBigNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
String message = "Hello welcome to the world of Android. I am very happy that you are here....";
builder.setSmallIcon(R.mipmap.barbell);
builder.setContentTitle("This is main Title");
builder.setContentText("This is the sub text");
builder.setTicker("This is ticker !!!");
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
builder.addAction(R.drawable.images, "Hi....", null);
builder.addAction(R.mipmap.barbell, "Hi....", null);
// builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(2, builder.build());
}
private void displaySimpleNotification() {
Log.i("TAG","Hello World");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.barbell);
builder.setContentTitle("This is mytitle");
builder.setContentText("Hi How are you");
builder.setTicker("This is ticker !!!");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.sNotification:
{
displaySimpleNotification();
break;
}
case R.id.bigNotification:
{
displayBigNotification();
break;
}
}
}
xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Notification"
android:id="@+id/sNotification"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="135dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Big Notification"
android:id="@+id/bigNotification"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
java
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_main);
按钮sNotification=(按钮)findViewById(R.id.sNotification);
Button bigNotification=(Button)findViewById(R.id.bigNotification);
sNotification.setOnClickListener(this);
setOnClickListener(this);
}
私有void displayBigNotification(){
NotificationCompat.Builder=新建NotificationCompat.Builder(此);
String message=“您好,欢迎来到Android世界。我很高兴您来到这里……”;
builder.setSmallIcon(R.mipmap.barbell);
builder.setContentTitle(“这是主标题”);
builder.setContentText(“这是子文本”);
setTicker(“这是ticker!!!”);
builder.setDefaults(Notification.DEFAULT\u ALL);
builder.setStyle(新的NotificationCompat.BigTextStyle().bigText(消息));
builder.addAction(R.drawable.images,“Hi…”,null);
builder.addAction(R.mipmap.barbell,“Hi…”,null);
//builder.setAutoCancel(true);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
notificationManager.notify(2,builder.build());
}
私有void displaySimpleNotification(){
Log.i(“标签”、“你好世界”);
NotificationCompat.Builder=新建NotificationCompat.Builder(此);
builder.setSmallIcon(R.mipmap.barbell);
builder.setContentTitle(“这是我的标题”);
setContentText(“你好”);
setTicker(“这是ticker!!!”);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
notificationManager.notify(1,builder.build());
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.sNotification:
{
displaySimpleNotification();
打破
}
案例R.id.BIG通知:
{
displayBigNotification();
打破
}
}
}
xml
编辑的代码看起来不错。只需将以下行添加到AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
这是因为NotificationManager正在使用振动功能。
希望这有帮助编辑的代码看起来不错。只需将以下行添加到AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
这是因为NotificationManager正在使用振动功能。
希望这有帮助两个onClick指向相同的方法-android:onClick=“simpleNotification”。你能再粘贴一点逻辑并告诉我们你是如何在它们之间进行选择的吗?谢谢你的回复。我以不同的方式编写了相同的程序。它给了我同样的问题我的朋友,现在它以java.lang.SecurityException的形式向我显示消息:需要振动权限。请你帮我一下好吗。谢谢。两个onClick指向相同的方法-android:onClick=“simpleNotification”。你能再粘贴一点逻辑并告诉我们你是如何在它们之间进行选择的吗?谢谢你的回复。我以不同的方式编写了相同的程序。它给了我同样的问题我的朋友,现在它以java.lang.SecurityException的形式向我显示消息:需要振动权限。请你帮我一下好吗。非常感谢。