2012-08-04 76 views
3

我创建自己的控制从TextView的延伸。 我尝试在RemoteViews通知中使用此控件。 但问题,如果我尝试显示通知,得到错误:ClassNotFoundException的RemoteViews

java.lang.ClassNotFoundException:{namespace}。{myClass}。

但如果我加入活动布局相同的控制。一切都好。 什么问题?

我的TextView:

public class ScrollingTextView extends TextView { 

public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public ScrollingTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public ScrollingTextView(Context context) { 
    super(context); 
} 

@Override 
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 
    if (focused) 
     super.onFocusChanged(focused, direction, previouslyFocusedRect); 
} 

@Override 
public void onWindowFocusChanged(boolean focused) { 
    if (focused) 
     super.onWindowFocusChanged(focused); 
} 

@Override 
public boolean isFocused() { 
    return true; 
} 

}

我通知布局:

<?xml version="1.0" encoding="utf-8"?> 

<ImageView 
    android:id="@+id/notification_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="10dp" 
    android:background="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/notification_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/notification_image" 
    android:text="notification_title" 
    android:textStyle="bold" /> 

<{namespace}.ScrollingTextView 
    android:id="@+id/notification_message" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/notification_title" 
    android:layout_toRightOf="@+id/notification_image" 
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:scrollHorizontally="true" 
    android:singleLine="true" 
    android:text="notification_message" /> 

通知呼叫:

Intent intent = new Intent(context, NotificationActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
Notification notification = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis()); 
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); 
RemoteViews contentView = new RemoteViews(_context.getPackageName(), R.layout.notify); 
contentView.setImageViewResource(R.id.notification_image, R.drawable.ic_launcher); 
contentView.setTextViewText(R.id.notification_title, state.contentTitle); 
contentView.setTextViewText(R.id.notification_message, state.contentText); 
notification.contentView = contentView; 
_notificationManager.notify(1, notification); 
  • UPDATE 2012年5月8日17时08

在Android清单我有值包= “com.test”。

在{命名空间}我understoo com.test,和{} myClass的 - ScrollingTextView。 将ScrollingTextView放置在包com.test处。

发生错误: android.view.InflateException:二进制XML文件行#25:错误膨胀类com.test.ScrollingTextView。

其中第25行是行,其中宣称ScollingTextView:

<com.test.ScrollingTextView ... 

完整的错误:

couldn't inflate view for notification com.test/0x1 
android.view.InflateException: Binary XML file line #25: Error inflating class com.test.ScrollingTextView 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
    at android.widget.RemoteViews.apply(RemoteViews.java:1580) 
    at com.android.systemui.statusbar.tablet.TabletStatusBar.inflateViews(TabletStatusBar.java:1938) 
    at com.android.systemui.statusbar.tablet.TabletStatusBar.addNotificationViews(TabletStatusBar.java:1744) 
    at com.android.systemui.statusbar.tablet.TabletStatusBar.addNotification(TabletStatusBar.java:891) 
    at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:231) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4424) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.ClassNotFoundException: com.test.ScrollingTextView 
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:552) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 
    ... 16 more 
+0

如何张贴一些代码,让我们别以为你有编码是什么? – Erol 2012-08-05 00:12:19

+0

更新第一篇文章。 如果我禁用设置ContenView,所有工作。 – 2012-08-05 05:32:53

+0

哪个类是{namespace}。{myClass}。?你在哪一行有错误? – Erol 2012-08-05 06:37:49

回答

6

远程视窗只能与被提及here标准Android意见屈指可数。特别是,你不能使用你自己的自定义视图。

这是因为远程视图实际上是被在不同的进程中运行,并且它没有访问您所定义的自定义视图类。

如果其他进程被允许加载和使用您的自定义视图,这将让你能够将任意代码到其他进程,实质上接管。

+0

感谢您的回答。我想移动到通知滚动文本视图(如果文本很长)。现在明白了,这是不可能的。 – 2012-08-05 22:52:34

+0

我有同样的问题,它也不适用'ConstraintLayout' – 2017-05-22 22:36:03