android - 使用 setContent 时自定义通知崩溃

标签 android eclipse android-layout notifications remoteview

PHP/C#/ASP 编码器在给 Xamarin 一个镜头后正确地开始在 android 中(然后有 6 个月的中断所有编码)。

我正在尝试在通知中实现自定义布局。我可以让它产生正常的通知,但是当它使用 setContent(view) 完成时我忍不住崩溃。

也是 Eclipse/ADT 的新手,所以很难知道如何自己解决问题(在 visual studio 中这很容易)。

澄清一下,displayNotification 当前设置为在应用启动时执行。

我确信它是如此简单和小,但是已经很晚了,我的头脑很疲惫。非常感谢所有帮助!

这是我的代码(删除了所有注释掉的代码只是为了清理它)。

protected void displayNotification() {

RemoteViews customNotificationView = 
new RemoteViews(getPackageName(), R.layout.notification_layout);

NotificationCompat.Builder  mBuilder = 
new NotificationCompat.Builder(this);   

mBuilder.setContent(customNotificationView);
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.ic_launcher);

mBuilder.setOngoing(true);

mNotificationManager = (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(notificationID, mBuilder.build());
}

这是我的自定义布局

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/notification_title"
    style="@style/NotificationTitle" 
    />


<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

最佳答案

我测试了您的代码,它似乎在布局中没有 SeekBar 的情况下工作正常。基于此answer您不能在通知布局上添加搜索栏。 我会看起来更好,稍后评论。

编辑: 您可以尝试将搜索栏更改为按钮并为其添加 onClick 监听器,这是一个起点。 Adding onClick Action To Button In Notification

关于android - 使用 setContent 时自定义通知崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23819429/

相关文章:

linux - Eclipse:如何在 Eclipse (Indigo) 中安装 Subclipse [在 Linux 上]

java - getResourceAsStream 返回 null,尽管被调用的文件与类 getResourceAsStream 位于同一目录中

android - 为什么 Android TextView 在单词开头错误地用点包裹文本(如文件扩展名)?

java.lang.ClassCastException : android. widget.RelativeLayout$LayoutParams 无法转换为 android.widget.FrameLayout$LayoutParams

Android,FragmentTransaction.add 之后的 fragment 状态

安卓 : Dimming down screen

AndroidTestCase assertActivityRequiresPermission() 断言不正确

android - ArrayAdapter 文本和图像

java - 使用 Ant 而不是 Eclipse 在 Java 中导入错误

android - 将布局动态添加到另一个布局