Android Question getting error when i compile this java code

Addo

Well-Known Member
Licensed User
Longtime User
B4X:
#If Java
import android.content.Context;
import android.content.SharedPreferences;


private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";

public synchronized static String id(Context context) {
   if (uniqueID == null) {
      SharedPreferences sharedPrefs = context.getSharedPreferences(
         PREF_UNIQUE_ID, Context.MODE_PRIVATE);
      uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
      if (uniqueID == null) {
         uniqueID = UUID.randomUUID().toString();
         Editor editor = sharedPrefs.edit();
         editor.putString(PREF_UNIQUE_ID, uniqueID);
         editor.commit();
      }
   }
    return uniqueID;
}


#End If

when i compile the above java code i got this compilation error

Compiling generated Java code. Error
src\mid\collsb\main.java:820: error: cannot find symbol
Editor editor = sharedPrefs.edit();
^
symbol: class Editor
location: class main
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

javac 11.0.1

what i am doing wrong ?
 

JordiCP

Expert
Licensed User
Longtime User
Try with
B4X:
SharedPreferences.Editor editor = sharedPrefs.edit();
or adding
B4X:
import android.content.SharedPreferences.Editor;
(Untested, but I guess that at least one should work)
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
i have tried

B4X:
#If Java
import android.content.Context;
import android.content.SharedPreferences.*;


private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";

public synchronized static String id(Context context) {
   if (uniqueID == null) {
      SharedPreferences sharedPrefs = context.getSharedPreferences(
         PREF_UNIQUE_ID, Context.MODE_PRIVATE);
      uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
      if (uniqueID == null) {
         uniqueID = UUID.randomUUID().toString();
         Editor editor = sharedPrefs.edit();
         editor.putString(PREF_UNIQUE_ID, uniqueID);
         editor.commit();
      }
   }
    return uniqueID;
}


#End If

when i run

jo.RunMethod("id", Null)

i got runtime exception

java.lang.RuntimeException: Method: id not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Try with

B4X:
...
Dim JContext as JavaObject
JCOntext.initializeContext
JO.RunMethod("id", Array(JContext))
...
 
Upvote 0
Top