android - 使用自定义按钮组件创建通知
问题描述
我必须设计一个带有自定义布局和组件的通知,例如作为基本组件编写的按钮和 textview。我用 RemoteView 处理了自定义视图端。我创建了自定义布局,并在此布局中创建了 Textview 和 Button。问题是我必须使用之前在我的框架中编写的基本组件中的按钮和文本视图,但是当我尝试使用它时,它会出错。
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.n9020732.remoteview, PID: 2945
android.app.RemoteServiceException: Bad notification posted from package com.example.n9020732.remoteview: Couldn't inflate contentViewsandroid.view.InflateException: Binary XML file line #39: Binary XML file line #39: Error inflating class com.example.n9020732.remoteview.AXButton
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我在此服务中创建通知。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("ONMESSAGERECEIVED", "Heloo");
if (remoteMessage.getData().size() > 0) {
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.remote_view);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentText("Content of the notification")
.setContentTitle("Title of the notification:")
.setCustomBigContentView(expandedView);
notificationManager.notify(0, builder.build());
}
}
}
这是基础组件。
public class AButton extends
android.support.v7.widget.AppCompatButton{
public AButton(Context context) {
super(context);
}
public AButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
远程视图.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content"/>
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bas bana!"/>
<com.example.n9020732.remoteview.AXButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basma!"/>
</LinearLayout>
我如何在通知中使用我的基本组件。
解决方案
推荐阅读
- java - 将字符串重新复制到新字符串对重新复制数组的性能影响更大吗?
- android - 拥有“发布”apk 后,我应该在 firebase 中删除 SHA1 调试密钥吗?
- java - 类型不匹配无法从可选类型转换
给用户 - linux - 以 ssh 登录时访问客户端计算机目录
- mysql - 如何在 SQL 子查询中使用 NOT IN?
- node.js - 在 nodejs 中创建 workerThread 时,它是否使用运行 nodejs 进程的相同内核?
- java - 你可以为 viewpager2 设置 OnClickListener 吗?
- java - 泛型类型的 Guava ClassToInstanceMap
- c# - 更改汉堡菜单图标 - Xamarin.Forms Android 和 UWP
- javascript - discord.js heroku 除了嵌入式命令外,一切正常,怎么了?