Java中自定义通知的标题已更改

时间:2018-07-17 19:21:57

标签: android-studio notifications textview

如何从自定义通知中更改textView? 我有这个生成器片段

builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setCustomContentView(remoteViews)
                    .setContentIntent(pendingIntent)
                    .setOngoing(true);

这是xml

<TextView
        android:id="@+id/txt_notif"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="46dp"
        android:layout_marginRight="46dp"
        android:text="@string/needTO"
        android:textColor="#ffffff"
        android:textSize="20sp" />

在Java中,我尝试使用此代码,但我的应用程序崩溃:

txt_notif = view.findViewById(R.id.txt_notif);
txt_notif.setText(getString(R.string.needTO));

错误在这里:

07-17 22:32:10.838 7630-7630/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.adisi.reminder, PID: 7630
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adisi.reminder/com.example.adisi.reminder.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                     at android.os.Handler.dispatchMessage(Handler.java:111)
                                                     at android.os.Looper.loop(Looper.java:194)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5653)
                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                     at com.example.adisi.reminder.SettingsFragment.onCreateView(SettingsFragment.java:58)
                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)

1 个答案:

答案 0 :(得分:0)

我为此找到了解决方案。我正在使用远程视图,所以我使用了它:

remoteViews.setTextViewText(R.id.txt_notif,getString(R.string.needTO));