android.support.v4.app.SuperNotCalledException

本文分享了在使用Fragment时遇到的一个特殊问题:SuperNotCalledException异常。详细介绍了该异常出现的原因及解决方法,强调了在覆写方法时正确调用父类方法的重要性。

        这是我写的第一篇博客,写博客是为了记住自己踩过的坑,让跟多的小朋友少走弯路。。。。。。

        之前有一次在写fragment的时候,遇到过这样一个问题:android.support.v4.app.SuperNotCalledException: Fragment ChartFrameActivity{42d15ec0 #0 id=0x7f080000 android:switcher:2131230720:0} did not call through to super.onDestroyView(),这个异常以前没有见过,看见他就一脸懵比不知所措。后来经过上网查询明白了一点:

主要总结原因有:1.分析问题不仔细,异常已经说明是super.onDestroyView有问题,没有看清楚,导致浪费时间。

                                 2.异常原因是因为在调用onDestroyView这个方法是将父方法给删除


希望以后引以为戒!!!也希望和各位一起学习!!!

`android.support.v4.app.NotificationManagerCompat` 是 Android Support Library 中的一个兼容类,用于以向后兼容的方式管理通知(Notifications)。它封装了 `NotificationManager` 的功能,使开发者可以在不同版本的 Android 系统上安全地发送和管理通知,而无需担心 API 版本差异。 --- ### ✅ 为什么使用 `NotificationManagerCompat`? - **兼容性**:支持从 API 4Android 1.6)开始的所有版本。 - **简化权限处理**:某些操作(如检查是否可以显示通知)在新版本中需要权限或设置,该类提供了统一接口。 - **避免崩溃**:避免因调用高版本 API 导致低版本系统崩溃。 --- ### 🧩 常见用途 1. 发送通知 2. 取消通知 3. 检查通知是否启用 4. 请求通知权限(间接配合) --- ### 💡 示例代码:使用 `NotificationManagerCompat` 发送通知 ```java import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; public class NotificationHelper { private static final String CHANNEL_ID = "my_channel_id"; private static final int NOTIFICATION_ID = 1; public static void createNotificationChannel(Context context) { // 必须为 Android O(API 26+)创建通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "My Channel"; String description = "Description of my channel"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); channel.setDescription(description); NotificationManager notificationManager = context.getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } public static void sendNotification(Context context) { // 创建通知渠道(必须先创建) createNotificationChannel(context); // 构建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) // 替换为你自己的图标 .setContentTitle("Hello") .setContentText("This is a test notification.") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true); // 点击后自动取消 // 设置点击通知后的意图(例如打开某个 Activity) Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE // Android 12+ 要求添加 FLAG_IMMUTABLE ); builder.setContentIntent(pendingIntent); // 使用 NotificationManagerCompat 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(NOTIFICATION_ID, builder.build()); } // 取消通知 public static void cancelNotification(Context context) { NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID); } // 检查通知是否被用户禁用(返回 false 表示无法显示) public static boolean areNotificationsEnabled(Context context) { return NotificationManagerCompat.from(context).areNotificationsEnabled(); } } ``` --- ### 🔍 关键点解释: | 代码部分 | 解释 | |--------|------| | `NotificationManagerCompat.from(context)` | 获取一个线程安全、兼容性的 `NotificationManagerCompat` 实例 | | `createNotificationChannel()` | Android 8.0+ 必须有通知渠道才能显示通知 | | `NotificationCompat.Builder` | 兼容性构建器,用于构造通知外观 | | `setSmallIcon()` | 必须设置,否则通知不会显示 | | `setAutoCancel(true)` | 用户点击后通知自动从状态栏移除 | | `PendingIntent` | 用于定义点击通知后的动作 | | `areNotificationsEnabled()` | 判断应用是否有权显示通知(用户是否关闭了通知) | --- ### ⚠️ 注意事项 - **targetSdkVersion ≥ 33 (Android 13)**:需要在 `AndroidManifest.xml` 中声明: ```xml <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> ``` 并在运行时请求该权限: ```java if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_CODE); } ``` - 图标资源建议放在 `res/drawable/` 或使用自适应图标(Adaptive Icon)。 - 使用 `PendingIntent.FLAG_IMMUTABLE` 是 Android 12+ 的要求,除非你明确需要修改 PendingIntent 内容。 --- ### ✅ 推荐迁移至 AndroidX > `android.support.v4.app.NotificationManagerCompat` 已被弃用。推荐迁移到 AndroidX: ```gradle implementation 'androidx.core:core:1.10.0' ``` 然后导入改为: ```java import androidx.core.app.NotificationManagerCompat; ``` 其余用法完全一致。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值