本文介绍了问题NotificationCompact.Builder和ActionBarSherlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code,Eclipse中发现了一个错误:

 的方法构建()是未定义的类型NotificationCompat.Builder
 

一切都添加 ActionBarSherlock 之前正常工作(以下的)

 进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.app.TaskStackBuilder;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.support.v4.app.NotificationCompat;

公共类NotificationActivity扩展的BroadcastReceiver {

    NotificationManager处;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        纳米=(NotificationManager)上下文
                .getSystemService(Context.NOTIFICATION_SERVICE);
        INT notifyID = 1;
        NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(
                上下文)
                .setSmallIcon(R.drawable.zcicon)
                .setAutoCancel(真)
                .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_LIGHTS)
                .setTicker(mytitle)。setContentTitle(为myContent)
                .setContentText(文文);
        意图resultIntent =新的意图(背景下,CalcareReader.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(上下文);
        //添加背部栈的意图(而不是意图本身)
        stackBuilder.addParentStack(MyActivity.class);
        //添加的活动开始到堆栈的顶部的意图
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        nm.notify(notifyID,mBuilder.build());这里//错误
    }
}
 

解决方案

构建()中添加了支持Android软件包的新版本

。取决于你如何获取和设置ActionBarSherlock,你可能会使用Android支持包的旧版本。请确保您有最新的一个在您的SDK管理器中下载,然后使用 Android的支持 - v4.jar 在这两个ActionBarSherlock项目和主应用程序的项目。

In the following code, Eclipse found an error:

The method build() is undefined for the type NotificationCompat.Builder

Everything worked fine before adding the ActionBarSherlock (following this tutorial).

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class NotificationActivity extends BroadcastReceiver {

    NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        int notifyID = 1;
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context)
                .setSmallIcon(R.drawable.zcicon)
                .setAutoCancel(true)
                .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_LIGHTS)
                .setTicker("mytitle").setContentTitle("mycontent")
                .setContentText("text, text");
        Intent resultIntent = new Intent(context, CalcareReader.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MyActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        nm.notify(notifyID, mBuilder.build()); // error here
    }
}
解决方案

build() was added in a newer edition of the Android Support package. Depending on how you obtained and set up ActionBarSherlock, you may be using an older edition of the Android Support package. Make sure that you have the latest one downloaded in your SDK Manager, then use that android-support-v4.jar in both the ActionBarSherlock project and your main application project.

这篇关于问题NotificationCompact.Builder和ActionBarSherlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:07