2014-03-06 61 views
0

我建一個通知:Android通知和的NoSuchMethodError

Notification.Builder builder = new Notification.Builder(
          getApplicationContext()) 
          .setTicker(
            getApplicationContext().getString(
              R.string.my_string)) 
          .setSmallIcon(android.R.drawable.sym) 
          .setContentTitle(
            getApplicationContext().getString(
              R.string.my_string_two)) 
          .setContentText(a.getB()) 
          .setSound(
            RingtoneManager 
              .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
          .setVibrate(new long[] { 10001000 }) 
          .setAutoCancel(true) 
          .setContentIntent(PendingIntent.getActivity(getApplicationContext(),0, 
                new Intent() 
                  .setAction(Intent.ACTION_VIEW) 
                  .setType(CallLog.Calls.CONTENT_TYPE) 
                  .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 
                0)); 
        NotificationManager nm = (NotificationManager) getApplicationContext() 
          .getSystemService(Context.NOTIFICATION_SERVICE); 
        nm.notify("interstitial_tag", 1, builder.build()); 

與Android 4.0我找到了一個錯誤:的NoSuchMethodError。 我該如何解決它?我是否使用Notification.Compact? 謝謝。

+1

請張貼滿例外,包括說哪種方法是異常消息未找到。 – laalto

+0

此外,擺脫'getApplicationContext()'並在此代碼段中的所有事件中使用'this'。當您知道*爲什麼*您正在使用'getApplicationContext()'時,只能使用'getApplicationContext()'。請參閱http://www.doubleencore.com/2013/06/context/ – CommonsWare

+0

該日誌表示該錯誤位於PendingIntent的行中。然後,我使用getApplicationContext(),因爲這在方法運行中調用,並且我不能使用「this」。 – user3253955

回答

0

你可以嘗試:

public static void sendNotification(Context context, String info){ 
     NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(context); 
//title notifications 
notifyBuilder.setContentTitle(context.getString(R.string.app_name)); 
//small icon 
notifyBuilder.setSmallIcon(R.drawable.ic_launcher); 
//set contentText 
notifyBuilder.setContentText(info); 
notifyBuilder.setVibrate(new long[]{100, 200, 100, 500});  notifyBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
//setAutoCancel 
notifyBuilder.setAutoCancel(true); 

getNotificationManager(context).notify(0, notifyBuilder.build()); 
} 

public final static NotificationManager getNotificationManager(Context context) { 
return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
} 

而當你使用一個新的Intent(),請插入一個目標類

Intent wrapperIntent = new Intent(context, SenderBroadcast.class); 
wrapperIntent.putExtra("KEY_UID", uid); 
wrapperIntent.setData(Uri.parse("senderbroadcast://"+uid)); 
wrapperIntent.setAction("REQUESTCODE_SENDERBROADCAST"); 
PendingIntent.getActivity(context, RequestCode.REQUESTCODE_SENDERBROADCAST, wrapperIntent, PendingIntent.FLAG_UPDATE_CURRENT);