如何在自定义通知文本视图中设置值
how to set the value in custom notification textview
请帮我在自定义通知中设置文本视图中的文本
private void startNotification()
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());
RemoteViews notificationView = new RemoteViews(getPackageName(),R.layout.mynotification);
mRemoteViews.setTextViewText(R.id.appName, getResources().getText(0,"gfhf"));
mBuilder.setContent(mRemoteViews);
Intent notificationIntent = new Intent(this, FlashLight.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
Intent switchIntent = new Intent(this, switchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.closeOnFlash, pendingSwitchIntent);
notificationManager.notify(1, notification);
}
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.RemoteViews.setTextViewText(int,
java.lang.CharSequence)' on a null object reference
请帮我解决这个问题。如何在 android 的通知中设置 textview 的值,在此先感谢。
检查这个:
private void startNotification() {
int icon = R.drawable.icon_48;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon_48);
contentView.setTextViewText(R.id.title, "Notification Title/App Name");
contentView.setTextViewText(R.id.text, "message");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
}
custom_notification.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/title"
style="Custom Notification Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:textSize="18sp"
android:textColor="#336ba4" />
<TextView
android:id="@+id/text"
style="Custom Notification Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_toRightOf="@id/image"
android:textSize="16sp"
android:textColor="#336ba4" />
</RelativeLayout>
请帮我在自定义通知中设置文本视图中的文本
private void startNotification()
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());
RemoteViews notificationView = new RemoteViews(getPackageName(),R.layout.mynotification);
mRemoteViews.setTextViewText(R.id.appName, getResources().getText(0,"gfhf"));
mBuilder.setContent(mRemoteViews);
Intent notificationIntent = new Intent(this, FlashLight.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
Intent switchIntent = new Intent(this, switchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.closeOnFlash, pendingSwitchIntent);
notificationManager.notify(1, notification);
}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RemoteViews.setTextViewText(int, java.lang.CharSequence)' on a null object reference
请帮我解决这个问题。如何在 android 的通知中设置 textview 的值,在此先感谢。
检查这个:
private void startNotification() {
int icon = R.drawable.icon_48;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon_48);
contentView.setTextViewText(R.id.title, "Notification Title/App Name");
contentView.setTextViewText(R.id.text, "message");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
}
custom_notification.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/title"
style="Custom Notification Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:textSize="18sp"
android:textColor="#336ba4" />
<TextView
android:id="@+id/text"
style="Custom Notification Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_toRightOf="@id/image"
android:textSize="16sp"
android:textColor="#336ba4" />
</RelativeLayout>