android致命异常:java.lang.NoClassDefFoundError

问题描述 投票:-3回答:1

我一直收到此错误,实际上,在手机上(在Android Studio上)编译时,我没有任何错误。请帮助我!

我正在与AlarmManager一起运行,以清除晚上12点(〜11:55)的sharedPreferences值。但是我在Firebase中遇到了这样的错误

enter image description here

    public class AlarmReceiver extends BroadcastReceiver {
    int notificationID;
    private SharedPreferences.Editor editor;
    private SharedPreferences.OnSharedPreferenceChangeListener sharedpreflistener;
    private SharedPreferences sharedPreferences;
    private String MAIN_DATA = "com.xxx.xxx.MAIN";


    public void waterDataClear() {
        editor = sharedPreferences.edit();
        editor.putFloat(WATER_DATA, 0);
        editor.putBoolean(WATER_DIALOG, true);
        editor.putInt(WATER_ML, 0);
        editor.apply();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) {
        sharedPreferences = context.getSharedPreferences(MAIN_DATA, Context.MODE_PRIVATE);

        Bundle extras = intent.getExtras();
        notificationID = Objects.requireNonNull(extras).getInt("NotificationID");

        if (notificationID == 10) {
            waterDataClear();
            notificationID = 0;
        }

    }
}

这是我的Manifest.XML文件。我还需要添加其他内容吗?

    <receiver android:name=".AlarmReceiver" />
    <receiver android:name=".WaterAlarm" />
    <receiver android:name=".DietAlarm" />
    <receiver android:name=".BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service
        android:name=".BootService"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BIND_JOB_SERVICE" />
java android broadcastreceiver
1个回答
0
投票

确保您在自己的清单文件中注册了BroadcastReceivers。

编辑

问题源自您的应用程序的通知。当用户单击通知时,它会触发广播接收者,后者又要启动该应用。看来您为广播接收器的注册未正确完成。看看android中的how to setup Broadcast Receivers以及如何设置广播接收器将响应的各种配置。

编辑

如果要使用AlarmManagers设置警报,并且一定要执行这些警报,那么它将被视为可延期的可执行任务,它属于WorkManager的范畴。 WorkManager创建肯定需要在应用程序关闭后执行的作业。代替使用BroadcastReceivers,使用WorkManager设置这些警报。

© www.soinside.com 2019 - 2024. All rights reserved.