Мне нужно создать уведомление с пользовательским макетом и компонентом, таким как кнопка и текстовое представление, которое написано как базовый компонент.Я обработал пользовательский вид с помощью 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>
Как использовать базовый компонент в уведомлении.