Java Question Problem with Java Code / Library

FabioG

Active Member
Licensed User
Longtime User
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

B4X:
java.lang.NullPointerException
how can I fix it?

Thank you and Merry Christmas
 
Last edited:

giga

Well-Known Member
Licensed User
Longtime User
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

B4X:
java.lang.NullPointerException
how can I fix it?

Thank you and Merry Christmas

Is this the complete error?
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi FabioG,
Do you have a example for bring this library to use? I need to learn more.
 

FabioG

Active Member
Licensed User
Longtime User
Hi FabioG,
Do you have a example for bring this library to use? I need to learn more.
that is only the first part of the library code

I would like to complete it in these days and being able to post in this forum

that code written on it for the moment only works if the activity is started with the screen off, if you turn on the screen and then off the LED stops flashing
 

giga

Well-Known Member
Licensed User
Longtime User
I think the forum will need more of the error code in order to help with this. java.lang.NullPointerException could be caused by any of the following

Per Oracle:

Thrown when an application attempts to use null in a case where an object is required. These include:
  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.
 

Theera

Well-Known Member
Licensed User
Longtime User
I can compile a library by slc, Ihave no error.
 

stevel05

Expert
Licensed User
Longtime User
My response has disappeared, I must have deleted it when changing a spelling error.

In FabioG's code mContext is not initialized, I suggested he try:

B4X:
private Context mContext = BA.applicationContext;

Which apparently worked.

Sorry for the deletion.
 

FabioG

Active Member
Licensed User
Longtime User
I have another problem
when call the function StartIconNotify app crash

this is the cose

I can fix ?

B4X:
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());
       
    }
   
}
 

stevel05

Expert
Licensed User
Longtime User
What is the error you are getting?
 

stevel05

Expert
Licensed User
Longtime User
I can't see that you've initialized builder!
 

FabioG

Active Member
Licensed User
Longtime User
I can't see that you've initialized builder!

this is the error :(

If use
B4X:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)

Eclipse return

B4X:
The constructor NotificationCompat.Builder(LedNotificationControl) is undefined
B4X:
** 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)
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
What version of Android have you configured in Tools/configure Paths?
 

stevel05

Expert
Licensed User
Longtime User
The constructor NotificationCompat.Builder(LedNotificationControl) is undefined

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.
 

FabioG

Active Member
Licensed User
Longtime User
Thanks, I tried but the same error: (
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.
 
Top