package net.snclab.lednotificationcontrol;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
@ShortName("LedNotificationControl")
@Author("FabioG")
@Version(1.0f)
public class LedNotificationControl {
private Context mContext;
public void LedFlashColor(int A, int R, int G, int B)
{
NotificationManager nm = ( NotificationManager ) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification();
notif.ledARGB = Color.argb(A, R, G, B);
notif.flags = Notification.FLAG_SHOW_LIGHTS; //Notification.DEFAULT_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
nm.notify(0, notif);
}
public void ClearLED()
{
NotificationManager nm = ( NotificationManager ) mContext.getSystemService( Context.NOTIFICATION_SERVICE );
nm.cancel(0);
}
}
java.lang.NullPointerException
I would like to create a library in order to flash the LED notification so colorful
I wrote this code in Eclips
B4X:package net.snclab.lednotificationcontrol; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.graphics.Color; import anywheresoftware.b4a.BA.Author; import anywheresoftware.b4a.BA.ShortName; import anywheresoftware.b4a.BA.Version; @ShortName("LedNotificationControl") @Author("FabioG") @Version(1.0f) public class LedNotificationControl { private Context mContext; public void LedFlashColor(int A, int R, int G, int B) { NotificationManager nm = ( NotificationManager ) mContext.getSystemService(Context.NOTIFICATION_SERVICE); Notification notif = new Notification(); notif.ledARGB = Color.argb(A, R, G, B); notif.flags = Notification.FLAG_SHOW_LIGHTS; //Notification.DEFAULT_LIGHTS; notif.ledOnMS = 100; notif.ledOffMS = 100; nm.notify(0, notif); } public void ClearLED() { NotificationManager nm = ( NotificationManager ) mContext.getSystemService( Context.NOTIFICATION_SERVICE ); nm.cancel(0); } }
but when I use it in B4A I get this error
how can I fix it?B4X:java.lang.NullPointerException
Thank you and Merry Christmas
Is this the complete error?
that is only the first part of the library codeHi FabioG,
Do you have a example for bring this library to use? I need to learn more.
private Context mContext = BA.applicationContext;
package net.snclab.lednotificationcontrol;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA;
@ShortName("LedNotificationControl")
@Author("FabioG")
@Version(1.0f)
public class LedNotificationControl {
private static final String Icon = null;
private Context mContext = BA.applicationContext;
NotificationCompat.Builder builder;
NotificationManager nm = ( NotificationManager ) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification();
/**
* A, B, R,G and B is ARGB Colors
* TimeOutONms is the Timeout for Led On in Milliseconds
* TimeOutOFFms is the Timeout for Led Off in Milliseconds
* NOTIFY_ID is the ID for the Notification
**/
public void LedFlashColor(int A, int R, int G, int B, int TimeOutONms, int TimeOutOFFms, int NOTIFY_ID)
{
notif.ledARGB = Color.argb(A, R, G, B);
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = TimeOutONms;
notif.ledOffMS = TimeOutOFFms;
nm.notify(NOTIFY_ID, notif);
}
public void ClearLED(int NOTIFY_ID)
{
NotificationManager nm = ( NotificationManager ) mContext.getSystemService( Context.NOTIFICATION_SERVICE );
nm.cancel(NOTIFY_ID);
}
public void StartIconNotify()
{
builder.setContentTitle("Notification")
.setContentText("ContentText")
.setTicker("TickerText")
.setSmallIcon(BA.applicationContext.getResources().getIdentifier(Icon, "drawable", BA.packageName))
.setWhen(System.currentTimeMillis());
nm.notify(1, builder.build());
}
}
I can't see that you've initialized builder!
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
The constructor NotificationCompat.Builder(LedNotificationControl) is undefined
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 283)
java.lang.NoClassDefFoundError: android.support.v4.app.NotificationCompat$Builder
at net.snclab.lednotificationcontrol.LedNotificationControl.StartIconNotify(LedNotificationControl.java:60)
at net.snclabs.TestAPP.main._activity_create(main.java:283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at net.snclabs.TestAPP.main.afterFirstLayout(main.java:98)
at net.snclabs.TestAPP.main.access$100(main.java:16)
at net.snclabs.TestAPP.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
What version of Android have you configured in Tools/configure Paths?
The constructor NotificationCompat.Builder(LedNotificationControl) is undefined
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(BA.applicationContext)
The constructor requires a Context again so try:
B4X:NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(BA.applicationContext)
Don't forget that the library will form part of the app in which it's being used which will be it's context when it's running.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?