2013-09-01 43 views
18

tôi cố gắng để tạo ra một thông báo tùy chỉnh nhưng nhận được ngoại lệ sau đây:Không thể mở rộng RemoteViews

FATAL EXCEPTION: main 
android.app.RemoteServiceException: Bad notification posted from package com.my.app: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.my.app user=UserHandle{0} id=1 tag=null score=0: Notification(pri=0 contentView=com.my.app/0x7f03001b vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1423) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5103) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 

Mã của tôi trông như thế này:

Intent intent = new Intent(this, MyFragmentActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent launchIntent = PendingIntent.getActivity(this, 0, intent, 0); 

RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.notification_layout); 
notificationView.setImageViewBitmap(R.id.notification_icon, icon); 
notificationView.setTextViewText(R.id.present_text, "Text Test"); 

Notification.Builder builder = new Notification.Builder(getApplicationContext()); 
builder 
    .setContentTitle("Titel Test") 
    .setSmallIcon(R.drawable.ic_launcher_icon) 
    .setContentIntent(launchIntent) 
    .setOngoing(true) 
    .setWhen(0) 
    .setTicker("ticker Test") 
    .setContent(notificationView); 

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(NOTIFICATION_ID, builder.getNotification()); 

Cách bố trí trông như sau:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/notification_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" /> 

    <EditText 
     android:id="@+id/present_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/notification_icon" 
     android:gravity="center_vertical" /> 

</RelativeLayout> 

Tôi đang chạy Android 4.3. Tôi đã thử trước với NotificationCompat và nhận được cùng một lỗi.

Có ai đó có ý tưởng không?

+0

Vui lòng đăng 'res/layout/notification_layout.xml'. – CommonsWare

+0

Tôi đã thêm bố cục. – samo

+1

Và đừng quên không đặt vectorDrawables để thông báo, nó là làm cho cùng một ngoại lệ. –

Trả lời

42

Bạn không thể đặt EditText vào RemoteViews. RemoteViews được giới hạn trong một số tiện ích con có thể, cụ thể là those documented for use on an app widget. Và, đối với một Notification, tôi nghi ngờ rằng các tùy chọn AdapterView, như ListView, cũng sẽ hoạt động.

+0

Cảm ơn. Thay thế EditText bằng TextView sẽ giải quyết nó. – samo

+0

Câu trả lời hoàn hảo !! –

3

Đối với tôi, đó là vì tôi đã đặt văn bản bên trong TextView từ xa không chính xác. Tôi đã làm điều này:

remoteViews.setString(R.id.txt, "setText", "text"); 

Sử dụng một trong những phương pháp giải quyết nó cho tôi:

remoteViews.setCharSequence(R.id.txt, "setText", "text"); 
//Or simply: 
remoteViews.setTextViewText(R.id.txt, "text"); 
+0

Sử dụng 'setCharSequence' thay vì' setTextViewText' làm việc cho tôi. – Miguelos

0

Không phải tất cả quan điểm có thể được sử dụng trong RemoteViews. Một đối tượng RemoteViews có thể hỗ trợ các lớp học bố trí như sau:

FrameLayout

LinearLayout

RelativeLayout

GridLayout

AnalogClock

Nút

Chronometer

ImageButton

ImageView

ProgressBar

TextView

ViewFlipper

ListView

GridView

StackView

AdapterViewFlipper

Bạn không thể sử dụng các lớp học bố trí khác như EditText hoặc CustomViews.

0

Tôi nhận thấy rằng việc sử dụng android: background = "? Attr/colorPrimaryDark" cũng gây ra sự cố. Khi được thay đổi thành android: background = "@ color/colorPrimaryDark", nó đã dừng hoạt động.

Các vấn đề liên quan