ClassNotFoundException的RemoteViews [英] ClassNotFoundException RemoteViews

查看:261
本文介绍了ClassNotFoundException的RemoteViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

抛出java.lang.ClassNotFoundException:{空间} {} MyClass的

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

我的TextView:

 公共类ScrollingTextView扩展的TextView {公共ScrollingTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
}公共ScrollingTextView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}公共ScrollingTextView(上下文的背景下){
    超级(上下文);
}@覆盖
保护无效onFocusChanged(布尔重点,诠释方向,矩形previouslyFocusedRect){
    如果(聚焦)
        super.onFocusChanged(突出重点,方向,previouslyFocusedRect);
}@覆盖
公共无效onWindowFocusChanged(布尔聚焦){
    如果(聚焦)
        super.onWindowFocusChanged(重点);
}@覆盖
公共布尔isFocused(){
    返回true;
}

}

我的通知布局:

 <?XML版本=1.0编码=UTF-8&GT?;

 < ImageView的
    机器人:ID =@ + ID / notification_image
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_centerVertical =真
    机器人:layout_marginRight =10dp
    机器人:背景=@绘制/ ic_launcher/><的TextView
    机器人:ID =@ + ID / notification_title
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_toRightOf =@ + ID / notification_image
    机器人:文字=notification_title
    机器人:文字样式=大胆/>< {命名空间} .ScrollingTextView
    机器人:ID =@ + ID / NOTIFICATION_MESSAGE
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / notification_title
    机器人:layout_toRightOf =@ + ID / notification_image
    机器人:ellipsize =金字招牌
    机器人:marqueeRepeatLimit =marquee_forever
    机器人:scrollHorizo​​ntally =真
    机器人:单线=真
    机器人:文字=NOTIFICATION_MESSAGE/>

电话通知:

 意向意图=新意图(背景下,NotificationActivity.class);
的PendingIntent的PendingIntent = PendingIntent.getActivity(背景下,1,意向,PendingIntent.FLAG_UPDATE_CURRENT);
通知通知=新的通知(R.drawable.ic_launcher,tickerText,System.currentTimeMillis的());
notification.setLatestEventInfo(背景下,contentTitle,contentText,的PendingIntent);
RemoteViews内容查看=新的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 =内容查看;
_notificationManager.notify(1,通知);


  • 更新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 ...

完整的错误:

 无法通知com.test /为0x1膨胀看法
android.view.InflateException:二进制XML文件行#25:错误充气类com.test.ScrollingTextView
    在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
    在android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    在android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    在android.widget.RemoteViews.apply(RemoteViews.java:1580)
    在com.android.systemui.statusbar.tablet.TabletStatusBar.inflateViews(TabletStatusBar.java:1938)
    在com.android.systemui.statusbar.tablet.TabletStatusBar.addNotificationViews(TabletStatusBar.java:1744)
    在com.android.systemui.statusbar.tablet.TabletStatusBar.addNotification(TabletStatusBar.java:891)
    在com.android.systemui.statusbar.CommandQueue $ H.handleMessage(CommandQueue.java:231)
    在android.os.Handler.dispatchMessage(Handler.java:99)
    在android.os.Looper.loop(Looper.java:137)
    在android.app.ActivityThread.main(ActivityThread.java:4424)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:511)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    在dalvik.system.NativeStart.main(本机方法)
抛出java.lang.ClassNotFoundException:引起com.test.ScrollingTextView
    在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    在android.view.LayoutInflater.createView(LayoutInflater.java:552)
    在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    ... 16个


解决方案

远程视窗仅适用于被提及的此处。特别是,你不能用它使用自己的自定义视图。

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

如果其他进程被允许加载和使用您的自定义视图,这将让你注入任意code到其他进程,基本上是抢过来。

I create own control extends from TextView. I try use this control in RemoteViews on notification. But problem, if I try show notify, get error:

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

But if I add same control in activity layout. All ok. What's the problem?

My 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;
}

}

My notify layout:

<?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" />

Notification call:

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 05.08.2012 17:08

In android manifest I have value package="com.test".

Under {namespace} I understoo com.test, and {myClass} - ScrollingTextView. ScrollingTextView placed at package com.test.

Error occured: android.view.InflateException: Binary XML file line #25: Error inflating class com.test.ScrollingTextView.

Where line 25 is line, where declared ScollingTextView:

<com.test.ScrollingTextView ...

Full error:

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

解决方案

RemoteView only works with the handful of standard android views that are mentioned here. In particular, you cannot use your own custom views with it.

This is because the remote view is actually being run in a different process, and it does not have access to the custom view classes that you have defined.

If the other process was allowed to load and use your custom view, this would allow you to inject arbitrary code into that other process, and essentially take it over.

这篇关于ClassNotFoundException的RemoteViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆