关于bignotification

时间:2015-11-02 23:11:38

标签: android

我已经在大通知上写了程序。当我运行它时,它给我结果,但消息为java.lang.IllegalStateException:无法执行android:onClick的方法。先感谢您。拜托,任何人都可以帮助我。以下是该计划。

    java
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_main);
        Button sNotification = (Button) findViewById(R.id.sNotification);
        Button bigNotification = (Button) findViewById(R.id.bigNotification);
        sNotification.setOnClickListener(this);
        bigNotification.setOnClickListener(this);
    }

    private void displayBigNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        String message = "Hello welcome to the world of Android. I am very happy that you are here....";
        builder.setSmallIcon(R.mipmap.barbell);
        builder.setContentTitle("This is main Title");
        builder.setContentText("This is the sub text");
        builder.setTicker("This is ticker !!!");
        builder.setDefaults(Notification.DEFAULT_ALL);
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        builder.addAction(R.drawable.images, "Hi....", null);
        builder.addAction(R.mipmap.barbell, "Hi....", null);
       // builder.setAutoCancel(true);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(2, builder.build());
    }

    private void displaySimpleNotification() {
        Log.i("TAG","Hello World");
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.barbell);
        builder.setContentTitle("This is mytitle");
        builder.setContentText("Hi How are you");
        builder.setTicker("This is ticker !!!");
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1,builder.build());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sNotification:
            {
                displaySimpleNotification();
                break;
            }
            case R.id.bigNotification:
            {
                displayBigNotification();
                break;
            }
        }
    }
        xml

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Simple Notification"
        android:id="@+id/sNotification"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="135dp"

        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Big Notification"
        android:id="@+id/bigNotification"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        />

1 个答案:

答案 0 :(得分:0)

编辑后的代码看起来不错。您只需将以下行添加到AndroidManifest.xml

<uses-permission android:name="android.permission.VIBRATE" />

这是因为NotificationManager正在使用振动功能。 希望这有帮助