我的问题是,当我试图从启动不同的阅读活动的复选框preference状态,然后发送一个状态栏通知。然后,当设备启动了,我得到一个强制关闭错误消息弹出然后当我进入错误日志,我不知道会发生什么。
My problem is that when I try to read a checkbox preference state from a different activity on bootup then send a status bar notification. Then when the device boots the I get a force close error message popup then when I go into the error log I don't understand what happens.
在code为广播接收机显示如下:
The code for the broadcast receiver is shown below:
@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);
}
}
}
所以,你能不能帮我解决,为什么在启动时它的力量关闭的问题。所以基本上我想要做的是阅读上启动一个复选框preference状态,然后发送一个状态栏通知。
So could you help me solve the issue on why it force closes on bootup. So basically what I want to do is read a checkbox preference state on bootup then send a status bar notification.
这是我的错误日志响应:
This is my error log response:
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
所以,我不知道该怎么在这里做的。
So I have no clue what to do from here.
好吧,我已经想通了,我做了什么错。这是我是如何连接上下文的notificationManger构造方法和意图构建方法是什么。
Alright I have figured out what I have done wrong. What it is how I was connecting the context to the notificationManger construct method and Intent construct method.
这是我的新的和修订code,它的工作原理: `公共类Bootu preceiver延伸的BroadcastReceiver {
Here is my new and revised code that works: `public class BootupReceiver extends 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浏览我以为张贴code可以正常工作这将是很好的我。
Since this question has gotten more than 100 views I thought it would be nice of me to post the code that works properly.
注:我不知道为什么结束的方括号的类没有显示与$ C $的其余部分C中,它是一个计算器误差
Note: I don't know why the closing curly bracket for the class isn't showing with the rest of the code it's a stackoverflow error
据日志('了java.lang.RuntimeException:无法实例接收器),系统无法创建您的接收器类的实例,因为系统无法找到指定的类(com.brandon.labs.nsettings.receivers.notifyBootup)。我想,这大概是你的AndroidManifest.xml文件的问题(接收器类),具有名字,它是不相关的preferences。
According to log ('java.lang.RuntimeException: Unable to instantiate receiver'), system is unable to create instance of your receiver class, because system is unable to find specified class (com.brandon.labs.nsettings.receivers.notifyBootup). I think it is probably problems with name (of receiver class) in your AndroidManifest.xml file, and it is not related to preferences.
在未来,如果你会得到另一个异常,我建议你仔细阅读信息除外)和堆栈跟踪。通常它们包含的答案你的问题的一半。 而对于流行的错误 - 你可以试着输入异常文本到谷歌(不包括一些特定的信息,例如类的名称),你会发现解决方案非常快
In future, if you will get another exception, i recommend you to read carefully message of exception ;) and stack trace. Usually they contains the half of answer to your question. And for popular mistakes - you can try to type exception text into Google (excluding some specific info such as name of class), and you will find solution very fast.