我有一个自定义通知布局,其中包含四个图像按钮。
当用户点击图像按钮时,我需要更改图像按钮的背景颜色。
我需要使用
ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
imgButton.setBackgroundColor(Color.BLUE);
但这会导致nullpointerexception错误,因为编译器无法找到recentAppButt图像按钮,因为这些图像按钮位于自定义通知布局中。
这是mainActivity.class中的一些代码,我在其中创建或删除通知
private void createNotificationIcons() {
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);
notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.home)
.setContentTitle("My notification")
.setContentText("All Buttons!")
.setOngoing(true) /** notification will appear as ongoing notification*/
.build();
/** set a custom layout to the notification in notification drawer */
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification);
notification.contentView = notificationView;
Intent recentAppIntent = new Intent(Context.NOTIFICATION_SERVICE);
recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle recentAppBundle = new Bundle();
recentAppBundle.putInt("userAnswer", 1);
recentAppIntent.putExtras(recentAppBundle);
PendingIntent pendingrecentAppIntent = PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
notificationManager.notify(1, notification);
}
public class SwitchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle answerBundle = intent.getExtras();
int userAnswer = answerBundle.getInt("userAnswer");
if(userAnswer == 1) {
ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
imgButton.setBackgroundColor(Color.BLUE);
Toast.makeText(context, "Recent Apps", Toast.LENGTH_SHORT).show();
Log.d("Recent", "recent app butt clicked");
openRecentApps();
}
}
}
public class BackgroundService extends Service {
MainActivity mainActivity = new MainActivity();
MainActivity.SwitchButtonListener switchButtonListener = mainActivity.new SwitchButtonListener();
/** Called when the service is being created. */
@Override
public void onCreate() {
super.onCreate();
}
/** The service is starting, due to a call to startService() */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(switchButtonListener, new IntentFilter(Context.NOTIFICATION_SERVICE));
return START_STICKY;
}
/** Called when The service is no longer used and is being destroyed */
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(switchButtonListener);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这就是notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@android:style/TextAppearance.StatusBar.EventContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >
<!-- create home button in notification drawer -->
<ImageButton
android:id="@+id/homeButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:layout_marginLeft="1dp"
android:background="@android:color/transparent"
android:contentDescription="@string/discription"
android:src="@drawable/home" />
<!-- create recent apps button in notification drawer -->
<ImageButton
android:id="@+id/recentAppButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:background="@android:color/transparent"
android:contentDescription="@string/discription"
android:src="@drawable/recent" />
<!-- create lock screen button in notification drawer -->
<ImageButton
android:id="@+id/lockScreenButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:background="@android:color/transparent"
android:contentDescription="@string/discription"
android:src="@drawable/lock" />
<!-- create allButtons button in notification drawer -->
<ImageButton
android:id="@+id/allButtonsButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:background="#FF990000"
android:contentDescription="@string/discription"
android:src="@drawable/all_buttons" />
提前致谢。
答案 0 :(得分:0)
检查您的布局文件中,您的ImageView是否具有设置为 wrap_parent 的高度和宽度参数。
如果其中任何一个(layout_height或layout_width)设置为 wrap_content ,并且您没有在ImageView上设置实际图像,则它的高度设置为0。 换句话说,您应该将wrap_content更改为另一个值,例如100dp,并查看颜色是否更改。
答案 1 :(得分:0)
findViewById 是活动类的一种方法,不适用于 RemoteViews 和通知。 您将获得零点异常,因为 R.id.recentAppButt 在您的活动视图中不存在。
您可以通过在drawables文件夹中创建具有不同背景颜色的两个图标来实现您想要的效果。然后,您可以使用 RemoteViews 类的 setImageViewResource 方法在两个图标之间切换,如下所示
isTrusted
然后,您需要重建通知并通过 NotificationManager 重新通知,以便更新背景颜色。