我的问题是,当我尝试在启动时从不同的活动中读取复选框首选项状态,然后发送状态栏通知。然后,当设备启动时,我会收到一个强制关闭错误消息,然后当我进入错误日志时,我不明白会发生什么。
广播接收器的代码如下所示:
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
//this creates a reference to my preferences activity
Prefs prefsC = new Prefs();
SharedPreferences prefs = context.getSharedPreferences("Prefs", 0);
int status = Integer.parseInt(prefs.getString("bootup", "-1"));
if(status > 0){
//notifyNS is a method that sends the status bar notification
prefsC.notifyNS("", R.drawable.n);
//the setCheckedNS method is just a custom method I made to set the state of a checkbox preference
prefsC.setCheckedNS("icon", false);
}else{
prefsC.setCheckedNS("enable", false);
prefsC.setCheckedNS("icon", false);
prefsC.setCheckedNS("bootup", false);
}
}
}
那么你能帮我解决为什么它强制关闭启动的问题。基本上我想要做的是在启动时读取复选框首选项状态,然后发送状态栏通知。
这是我的错误日志响应:
04-16 11:23:15.546: ERROR/AndroidRuntime(977): FATAL EXCEPTION: main
04-16 11:23:15.546: ERROR/AndroidRuntime(977): java.lang.RuntimeException: Unable to instantiate receiver com.brandon.labs.nsettings.receivers.notifyBootup: java.lang.ClassNotFoundException: com.brandon.labs.nsettings.receivers.notifyBootup in loader dalvik.system.PathClassLoader[/data/app/com.brandon.labs.nsettings-1.apk]
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2913)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.access$3200(ActivityThread.java:135)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2198)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.os.Looper.loop(Looper.java:144)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.main(ActivityThread.java:4937)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at dalvik.system.NativeStart.main(Native Method)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): Caused by: java.lang.ClassNotFoundException: com.brandon.labs.nsettings.receivers.notifyBootup in loader dalvik.system.PathClassLoader[/data/app/com.brandon.labs.nsettings-1.apk]
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2904)
04-16 11:23:15.546: ERROR/AndroidRuntime(977): ... 10 more
所以我不知道该怎么做。
好吧,我已经弄清楚我做错了什么。我是如何将上下文连接到notificationManger构造方法和Intent构造方法的。
以下是我的新修订代码: `public class BootupReceiver扩展了BroadcastReceiver {
private static final boolean BOOTUP_TRUE = true;
private static final String BOOTUP_KEY = "bootup";
@Override
public void onReceive(Context context, Intent intent) {
if(getBootup(context)) {
Toast toast2 = Toast.makeText(context, "getBootup", Toast.LENGTH_SHORT);
toast2.show();
NotificationManager NotifyM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification Notify = new Notification(R.drawable.n,
"NSettings Enabled", System.currentTimeMillis());
Notify.flags |= Notification.FLAG_NO_CLEAR;
Notify.flags |= Notification.FLAG_ONGOING_EVENT;
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
Notify.contentView = contentView;
Intent notificationIntent = new Intent(context, Toggles.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notify.contentIntent = contentIntent;
Toast toast = Toast.makeText(context, "Notify about to be sent", Toast.LENGTH_SHORT);
toast.show();
int HELO_ID = 00000;
NotifyM.notify(HELLO_ID, Notify);
Toast toast1 = Toast.makeText(context, "Notify sent", Toast.LENGTH_SHORT);
toast1.show();
}
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.brandon.labs.nsettings.NotifyService");
context.startService(serviceIntent);
}
public static boolean getBootup(Context context){
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(BOOTUP_KEY, BOOTUP_TRUE);
}
} `
由于此问题已获得超过100个观看次数,因此我认为发布正常运行的代码会很好。
注意:我不知道为什么该类的结束花括号没有显示其余的代码,这是一个stackoverflow错误
答案 0 :(得分:1)
根据log('java.lang.RuntimeException:无法实例化接收者'),系统无法创建接收者类的实例,因为系统无法找到指定的类(com.brandon.labs.nsettings.receivers) .notifyBootup)。我认为这可能是AndroidManifest.xml文件中名称(接收者类)的问题,并且与首选项无关。
将来,如果您将获得另一个异常,我建议您仔细阅读异常消息;)和堆栈跟踪。通常它们包含您问题答案的一半。 对于流行的错误 - 您可以尝试在Google中输入异常文本(不包括某些特定信息,例如课程名称),您会很快找到解决方案。
答案 1 :(得分:1)
您是否已将以下内容添加到AndroidManifest.xml并注册BroadcastReceiver
??
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />