我必须设计一个具有自定义布局和组件(例如按钮和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);
}
remote_view.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>
我如何在通知中使用我的基本组件。