Android Question error java.lang.ClassNotFoundException in InitializeNewInstance

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

public class thisTest{

public thisTest(){

}
}
#End If

Now I want to call and use this class with javaobject library:
B4X:
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
B4X:
#if java
public static class thisTest{
...
#end if
not this time, it seems. did you try it?

this simple example:
B4X:
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?

DonManfred

Expert
Licensed User
Longtime User

Just use static methods in java, not classes. It´s quite easy to call them.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
what @DonManfred recommends, plus java is case-sensitive: "thistest" is not the same as "thisTest" in java.

B4X:
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:
    Dim jo As JavaObject
    jo.InitializeNewInstance(Application.PackageName & [B]".main$thisTest[/B]",Array(Null))
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Make the class static
B4X:
#if java
public static class thisTest{
...
#end if
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Make the class static
B4X:
#if java
public static class thisTest{
...
#end if
not this time, it seems. did you try it?

this simple example:
B4X:
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

  • classtest.zip
    7.2 KB · Views: 117
Upvote 0
Solution

Daestrum

Expert
Licensed User
Longtime User
This is my java code
B4X:
public static class thisTest{
    public thisTest(){}
    
    public void say(){
        BA.Log("here");
    }
}

And this is the code in B4A (B4XPages)
B4X:
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

Mitwellnessers

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

public class thisTest{

public thisTest(){

}
}
#End If

Now I want to call and use this class with javaobject library:
B4X:
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
Top