我创建自己的控件从 TextView 扩展。我尝试在通知时在 RemoteViews 中使用此控件。但是问题,如果我尝试显示通知,则会出现错误:
java.lang.ClassNotFoundException: {namespace}.{myClass}。
但是如果我在活动布局中添加相同的控件。一切都好。有什么问题?
我的文本视图:
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);
- 更新 05.08.2012 17:08
在 android 清单中,我有值 package="com.test"。
在 {namespace} 我理解 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