Android Question Inline Java Code Error

Juan Marrero

Active Member
Licensed User
Longtime User
Is there something wrong with this code?

B4X:
#Region  Project Attributes
   #ApplicationLabel: testNFC
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: portrait
   #CanInstallToExternalStorage: False
   #AdditionalJar: android-support-v4
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim jo As JavaObject
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Private btnToggle As Button
   Private lblTitle As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Main")
  
   If FirstTime Then
     jo.InitializeContext
   End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnToggle_Click
   Dim isOn As Boolean
   Dim success As Boolean
  
   isOn = jo.RunMethod("checkNfcPowerStatus", Array As Object(Me))
   success = jo.RunMethod("powerNfc", Array As Object(isOn, Me))
  
   If success Then
     ToastMessageShow("Success", False)
   Else
     ToastMessageShow("Failed", False)
   End If
End Sub

#If JAVA
   public static boolean powerNfc(boolean isOn, Context context) {
    boolean success = false;
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);

    if (nfcAdapter != null) {
    Class<?> NfcManagerClass;
    Method setNfcEnabled;

    try {
    NfcManagerClass = Class.forName(nfcAdapter.getClass().getName());
    setNfcEnabled = NfcManagerClass.getDeclaredMethod(isOn
    ? "enable" : "disable");
    setNfcEnabled.setAccessible(true);
    success = (Boolean) setNfcEnabled.invoke(nfcAdapter);
    } catch (ClassNotFoundException e) {
    Log.e(TAG, Log.getStackTraceString(e));
    } catch (NoSuchMethodException e) {
    Log.e(TAG, Log.getStackTraceString(e));
    } catch (IllegalArgumentException e) {
    Log.e(TAG, Log.getStackTraceString(e));
    } catch (IllegalAccessException e) {
    Log.e(TAG, Log.getStackTraceString(e));
    } catch (InvocationTargetException e) {
    Log.e(TAG, Log.getStackTraceString(e));
    }
    }

    return success;
   }

   public static boolean checkNfcPowerStatus(Context context) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
    boolean enabled = false;

    If (nfcAdapter != Null) {
    enabled = nfcAdapter.isEnabled();
    }

    Return enabled;
   }
#End If

Any help is appreciated.

This is the error:
Compiling generated Java code. Error
javac 1.8.0_101
src\prusb\nfctoggle\main.java:478: error: ';' expected
If (nfcAdapter != Null) {
^
1 error
 

Juan Marrero

Active Member
Licensed User
Longtime User
thanks for your response, corrected that "if" and now i got this:

Compiling generated Java code. Error
B4A line: 59
End Sub
javac 1.8.0_101
src\prusb\nfctoggle\main.java:444: error: cannot find symbol
public static boolean powerNfc(boolean isOn, Context context) {
^
symbol: class Context
location: class main

:(

I copy/paste this code from the internet. Very few knowledge of Java.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
It seems that does not recognize the Context class. You will have to add, at the beginning of the inline java part

B4X:
#if JAVA
  import android.content.Context;

  //...

There are two kinds of errors which easily appear when using Inline Java

> Java is case sensitive
> When it does not recognize a Class, it is because it doesn't know where to look for it. I usually google "android developer" plus the keyword, and usually you get at the beginning of the page the package where it is declared. Then you add the "import...." sentence and it will be recognized.;)
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Excellent tip. Added import android.content.Context;. Also I added import android.nfc.NfcAdapter; and import java.lang.reflect.Method; because related errors appeared but still get error on nfcAdapter:

Compiling generated Java code. Error
javac 1.8.0_101
src\prusb\nfctoggle\main.java:484: error: cannot find symbol
if (nfcAdapter != Null) {
^
symbol: variable Null
location: class main
Note: src\prusb\nfctoggle\starter.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

My phone doesn't want to turn on NFC, i found this code to try to turn it on by code. Possibly NFC chip is damaged but I wanted to be sure. Phone is rooted.
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Yep, all fixed now. Also the word Return is capitalized. Also added this line private static final String TAG = "testNFC";. You are a life saver. Thank you JordiCP.
 
Upvote 0
Top