1

Ich kann nicht herausfinden, warum dieser Fehler auftritt. Ich erstelle eine Alarmschaltfläche, die eine Benachrichtigung an den Benutzer VIA sendet.Android Studio Erwartete Klasse oder Paket Fehler

"neue NotificationCompat()" ist, wo mein Fehler ist.

hier ist der Code:

public class AlertReceiver extends BroadcastReceiver{ 
 
    @Override 
 
    public void onReceive(Context context, Intent intent) { 
 

 
     createNotification(context, "It Is Time To Stand Up!","20 Minutes Have Passed", "Alert"); 
 

 
    } 
 

 
    public void createNotification(Context context, String msg, String msgText, String msgAlert) { 
 

 
     PendingIntent notificIntent = PendingIntent.getActivity(context,0,new Intent(context,CustomizeAlertsPage.class),0); 
 

 
     NotificationCompat.Builder mBuilder = new NotificationCompat().Builder(context) 
 
       .setLargeIcon(R.drawable.ALERT) 
 
       .setContentTitle(msg) 
 
       .setTicker(msgAlert) 
 
       .setContentText(msgText); 
 

 
     mBuilder.setContentIntent(notificIntent); 
 
     mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE); 
 
     mBuilder.setAutoCancel(true); 
 

 
     NotificationManager mNotificationManager = 
 
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
 

 
     mNotificationManager.notify(1,mBuilder.build()); 
 

 
    } 
 
}

Antwort

2

ändern

NotificationCompat.Builder mBuilder = new NotificationCompat().Builder(context) 

zu

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
0

new NotificationCompat().Builder() erstellt zuerst NotificationCompat Klasseninstanz und dann versuchen, etwas mit dem Namen Builder darin zu finden.

Sie müssen diese Zeile wie folgt umschreiben: new NotificationCompat.Builder() so dass new die Instanz in der NotificationCompat erklärt Builder Klasse erstellt.