Android Question error java.lang.ClassNotFoundException in InitializeNewInstance

hi dears,
I made this class for testing:
B4X: Copy
#if java

public class thisTest{

public thisTest(){

}
}
#End If

Now I want to call and use this class with javaobject library:
B4X: Copy
Dim j2 As JavaObject=Me
 j2.InitializeNewInstance(Application.PackageName & "." & "thistest",Array(Null))

But I get the following error:
java.lang.ClassNotFoundException: b4a$example$thistest
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:274)
 
Solution
Make the class static
Code: Copy
#if java
public static class thisTest{
...
#end if
not this time, it seems. did you try it?

this simple example:
B4X: Copy
Sub Activity_Create(FirstTime As Boolean)
    
    jo.InitializeNewInstance(Application.PackageName & ".main$thisTest",Array(Null))
    
End Sub


#if Java

public class thisTest{
    public thisTest(){
        BA.Log("here");
    }
}
#End If

runs for me (b4a 12.80, android 12). if i make the class static (which is what i normally would have done), i get a constructor not found error. i switched back and forth a number of times to make sure my eyes weren't up to their usual tricks. can you check it out?

Just use static methods in java, not classes. It´s quite easy to call them.
 
Upvote 0
what @DonManfred recommends, plus java is case-sensitive: "thistest" is not the same as "thisTest" in java.

B4X: Copy
Dim j2 As JavaObject=Me
 j2.InitializeNewInstance(Application.PackageName & "." & "[B]thistest[/B]",Array(Null))

public class [B]thisTest[/B]{

also, your instantiation is wrong:
B4X: Copy
    Dim jo As JavaObject
    jo.InitializeNewInstance(Application.PackageName & [B]".main$thisTest[/B]",Array(Null))
 
Upvote 0
Make the class static
Code: Copy
#if java
public static class thisTest{
...
#end if
not this time, it seems. did you try it?

this simple example:
B4X: Copy
Sub Activity_Create(FirstTime As Boolean)
    
    jo.InitializeNewInstance(Application.PackageName & ".main$thisTest",Array(Null))
    
End Sub


#if Java

public class thisTest{
    public thisTest(){
        BA.Log("here");
    }
}
#End If

runs for me (b4a 12.80, android 12). if i make the class static (which is what i normally would have done), i get a constructor not found error. i switched back and forth a number of times to make sure my eyes weren't up to their usual tricks. can you check it out?
 

Attachments

Upvote 0
Solution
This is my java code
Code: Copy
public static class thisTest{
    public thisTest(){}
    
    public void say(){
        BA.Log("here");
    }
}

And this is the code in B4A (B4XPages)
Code: Copy
Private Sub Button1_Click
    Dim myClass As JavaObject
    myClass.InitializeNewInstance("b4a.example.main$thisTest",Null)
    myClass.RunMethod("say",Null)
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub
 
Upvote 0
hi dears,
I made this class for testing:
B4X: Copy
#if java

public class thisTest{

public thisTest(){

}
}
#End If

Now I want to call and use this class with javaobject library:
B4X: Copy
Dim j2 As JavaObject=Me
 j2.InitializeNewInstance(Application.PackageName & "." & "[URL='https://berufshaftpflichtversicherungen-heute.de/wellness/']t[/URL]histest",Array(Null))

But I get the following error:
Thanks a lot
 
Upvote 0
Back
Top