【问题标题】:Custom Notification crashes when using setContent使用 setContent 时自定义通知崩溃
【发布时间】:2014-05-23 10:01:00
【问题描述】:

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>

【问题讨论】:

    标签: android eclipse android-layout notifications remoteview


    【解决方案1】:

    我测试了您的代码,并且在布局中没有 SeekBar 的情况下它似乎可以正常工作。基于此answer,您无法在通知布局上添加搜索栏。 我会更好看,稍后发表评论。

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

    【讨论】:

    • 谢谢!我什么时候可以打开我的电脑看看。我从未考虑过更改要测试的 xml(真的有点傻……但我学习了)。从我读过的内容来看,这绝对是可能的,但似乎比我预期的要复杂。无论哪种方式,都是一个很好的起点。谢谢。
    • 正如你所说 - 删除搜索栏,然后它就可以工作了。我花了一些时间环顾四周,当谈到搜索栏时,它似乎是一堵砖墙......但是我确实注意到一个应用程序在通知中复制了搜索栏。 link。玩这个,你不能滑动,但是你可以按不同的位置 - 所以我想这是使用按钮和以某种方式看起来像搜索栏的情况。这很可能是我现在的方向。
    • 我也看过一些应用程序并注意到同样的事情,但我认为这不是一件容易的事。你可以做两个按钮“向上”和“向下”,并以百分比显示进度。你能标记为正确答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多