如何在自定义通知textview中设置值

techandroid07

请帮助我在自定义通知中的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:尝试在空对象引用上调用虚拟方法“ void android.widget.RemoteViews.setTextViewText(int,java.lang.CharSequence)”

请帮我解决这个问题。如何在android的通知中设置textview的值,预先感谢。

乔杜里·阿马尔(Chaudhary Amar)

检查一下:

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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

如何在排球请求中设置自定义标题

来自分类常见问题

如何在Exoplayer通知中设置自定义布局和字体?

来自分类Dev

如何在图表中设置自定义填充颜色

来自分类Dev

如何在CreateJs中设置自定义光标?

来自分类Dev

如何在自定义WebViewPage中设置属性?

来自分类Dev

如何在自定义SNMP MIB中获取和设置OID值

来自分类Dev

如何在QML中设置自定义滑块的初始值?

来自分类Dev

如何为报表中的记录设置自定义值?

来自分类Dev

如何在RecyclerView中为TextView设置自定义字体?

来自分类Dev

如何在JCalendar中设置自定义星期开始?

来自分类Dev

如何在Android中制作自定义TextView?

来自分类Dev

如何在Maven中设置JDK的自定义版本?

来自分类Dev

如何在HTML中设置自定义tabindex

来自分类Dev

如何在Helmchart中设置自定义发布名称

来自分类Dev

如何在C ++中设置Actor的“渲染自定义深度传递”和“深度模板值”?

来自分类Dev

如何在SwiftUI中设置自定义环境键?

来自分类Dev

如何在Flutter中的背景上设置自定义声音通知?

来自分类Dev

如何在自定义HttpMessageHandler中设置属性?

来自分类Dev

如何在Flutter中设置自定义FontWeight值

来自分类Dev

Android如何在画布中设置自定义字体?

来自分类Dev

如何在Android中创建自定义通知

来自分类Dev

如何在Java中设置自定义光标?

来自分类Dev

如何在LayoutInflater中自定义TextView

来自分类Dev

如何在Spring Tool Suite中设置自定义JRE系统库路径值?

来自分类Dev

如何在Android的“自定义对话框”中设置TextView的值

来自分类Dev

如何在RecyclerView中为TextView设置自定义字体?

来自分类Dev

如何在自定义通知textview中设置值

来自分类Dev

如何在 Android 的通知中显示自定义文本

来自分类Dev

如何在 Dynamics 中创建自定义通知

Related 相关文章

  1. 1

    如何在排球请求中设置自定义标题

  2. 2

    如何在Exoplayer通知中设置自定义布局和字体?

  3. 3

    如何在图表中设置自定义填充颜色

  4. 4

    如何在CreateJs中设置自定义光标?

  5. 5

    如何在自定义WebViewPage中设置属性?

  6. 6

    如何在自定义SNMP MIB中获取和设置OID值

  7. 7

    如何在QML中设置自定义滑块的初始值?

  8. 8

    如何为报表中的记录设置自定义值?

  9. 9

    如何在RecyclerView中为TextView设置自定义字体?

  10. 10

    如何在JCalendar中设置自定义星期开始?

  11. 11

    如何在Android中制作自定义TextView?

  12. 12

    如何在Maven中设置JDK的自定义版本?

  13. 13

    如何在HTML中设置自定义tabindex

  14. 14

    如何在Helmchart中设置自定义发布名称

  15. 15

    如何在C ++中设置Actor的“渲染自定义深度传递”和“深度模板值”?

  16. 16

    如何在SwiftUI中设置自定义环境键?

  17. 17

    如何在Flutter中的背景上设置自定义声音通知?

  18. 18

    如何在自定义HttpMessageHandler中设置属性?

  19. 19

    如何在Flutter中设置自定义FontWeight值

  20. 20

    Android如何在画布中设置自定义字体?

  21. 21

    如何在Android中创建自定义通知

  22. 22

    如何在Java中设置自定义光标?

  23. 23

    如何在LayoutInflater中自定义TextView

  24. 24

    如何在Spring Tool Suite中设置自定义JRE系统库路径值?

  25. 25

    如何在Android的“自定义对话框”中设置TextView的值

  26. 26

    如何在RecyclerView中为TextView设置自定义字体?

  27. 27

    如何在自定义通知textview中设置值

  28. 28

    如何在 Android 的通知中显示自定义文本

  29. 29

    如何在 Dynamics 中创建自定义通知

热门标签

归档