2014-03-12 32 views
5

我正在使用動態佈局而不是遠程視圖中的xml佈局創建自定義通知,但最終會出現以下錯誤。遠程視圖中動態佈局的自定義通知

03-12 14:42:59.907: E/AndroidRuntime(2399): android.app.RemoteServiceException: Bad notification posted from package com.example.notificationexample: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.example.notificationexample id=0 tag=null score=0 notn=Notification(pri=0 contentView=com.example.notificationexample/0x1e vibrate=null sound=null defaults=0x0 flags=0x10 kind=[null])) 
03-12 14:42:59.907: E/AndroidRuntime(2399):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 
03-12 14:42:59.907: E/AndroidRuntime(2399):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-12 14:42:59.907: E/AndroidRuntime(2399):  at android.os.Looper.loop(Looper.java:137) 
03-12 14:42:59.907: E/AndroidRuntime(2399):  at android.app.ActivityThread.main(ActivityThread.java:4905) 
03-12 14:42:59.907: E/AndroidRuntime(2399):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-12 14:42:59.907: E/AndroidRuntime(2399):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-12 14:42:59.907: E/AndroidRuntime(2399):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 

我已經看到了許多問題,幾乎相同的問題,但他們在佈局中使用xml。我正在使用動態佈局。這裏是我的代碼:

public void CustomNotification() { 


     //creating Custom Layout 
     RelativeLayout parent_layout=new RelativeLayout(this); 
     RelativeLayout.LayoutParams rlparent = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.MATCH_PARENT); 

     parent_layout.setLayoutParams(rlparent); 
     parent_layout.setBackgroundColor(Color.WHITE); 
     parent_layout.setId(30); 

     //creating image 
     ImageView iv=new ImageView(this); 
     RelativeLayout.LayoutParams rl_iv=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     iv.setId(31); 
     iv.setImageResource(R.drawable.androidhappy); 
     iv.setLayoutParams(rl_iv); 
     parent_layout.addView(iv); 

     //creataing text view "title" 

     TextView title = new TextView(this); 
     RelativeLayout.LayoutParams title_Param = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
     title_Param.addRule(RelativeLayout.RIGHT_OF,iv.getId()); 
     title_Param.addRule(RelativeLayout.ALIGN_LEFT); 
     // title.setText("Title"); 
     title.setEllipsize(TextUtils.TruncateAt.END); 
     title.setPadding(6, 5, 0, 0); 
     title.setSingleLine(true); 
     title.setTextColor(Color.BLACK); 
     title.setTextSize(12); 
     title.setText("Good Morning"); 
     title.setId(11); 
      title.setLayoutParams(title_Param); 
     parent_layout.addView(title); 


     // Using RemoteViews to bind custom layouts into Notification 
     RemoteViews remoteViews = new RemoteViews(this.getPackageName(),parent_layout.getId()); 
     // RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.orignal_customnotification); 

     // Set Notification Title 
     String strtitle = getString(R.string.customnotificationtitle); 
     // Set Notification Text 
     String strtext = getString(R.string.customnotificationtext); 

     // Open NotificationView Class on Notification Click 
     Intent intent = new Intent(this, NotificationView.class); 
     // Send data to NotificationView Class 
     intent.putExtra("title", strtitle); 
     intent.putExtra("text", strtext); 
     // Open NotificationView.java Activity 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(
       this) 
     // Set Icon 
       .setSmallIcon(R.drawable.logosmall) 
       // Set Ticker Message 
       .setTicker("My Ticker text...") 
       // Dismiss Notification 
       .setAutoCancel(true) 
       // Set PendingIntent into Notification 
       .setContentIntent(pIntent) 
       // Set RemoteViews into Notification 
       .setContent(remoteViews); 


     remoteViews.setImageViewResource(iv.getId(),R.drawable.androidhappy); 
     remoteViews.setTextViewText(title.getId(), "Good Morning.."); 


// Create Notification Manager 
     NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     // Build Notification with Notification Manager 
     notificationmanager.notify(0, builder.build()); 


    } 

回答

1

你想要做什麼是不可能的;生活View對象不能與通知一起使用; RemoteViewsNotification.contentView支持的唯一類型。如果你看看API docs,你可以看到創建一個的唯一方法是引用一個佈局xml文件,但是一旦你有了一個,你可以用很多不同的方式修改它,包括將其他RemoteViews添加到佈局。