Skip to content
Advertisement

How can I run a java.class from Main Activity

I’m a beginner at Android Development and hoping someone can help me a bit out.

I want to connect to a local server (IP). I found a code in GitHub that supposedly would do this connection. But the thing is that this is a java.class and not in my MainActivity. So when I run my app in the emulator now, nothing happens. How can I run the Java.class from inside my MainActivity?

Here is the source: https://github.com/calimero-project/introduction/tree/master/src/main/java

Class:

JavaScript

MainActivity:

JavaScript

Advertisement

Answer

Try this

JavaScript

This should work if the code is correct. Think of the onCreate function of your Activities as the main function. You don’t need another class for a task that small.

Or else, you can otherwise also just create an instance of that class inside your onCreate method in the MainActivity and just call the main method that establishes the connection from that class with the object.

Either way, you’ll be able to do it.

You can learn more about the basics of Android Development, Activity, and Android Activity lifecycle so that you can understand this better.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement