2015-11-02 1 views
1

J'ai écrit le programme sur une grosse notification. Quand je cours il me donne le résultat mais avec le message comme java.lang.IllegalStateException: N'a pas pu exécuter la méthode pour android: onClick. Merci d'avance. S'il vous plaît, quelqu'un pourrait-il m'aider dans cette affaire? Voici le programme.En ce qui concerne bignotification

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" 
     /> 
+1

les deux onClicks pointent vers la même méthode - android: onClick = "simpleNotification". Pouvez-vous coller un peu plus de la logique et nous dire comment vous choisissez entre eux? – Actiwitty

+0

Merci pour la réponse. J'ai écrit le même programme différemment. S'il vous plaît, pourriez-vous vérifier. –

+0

il me donne même genre de problème –

Répondre

0

Le code édité semble bon. Il vous suffit d'ajouter la ligne suivante à votre AndroidManifest.xml

<uses-permission android:name="android.permission.VIBRATE" /> 

En effet, le NotificationManager utilise la fonction vibreur. Espérons que cela aide