0

I am trying to make an application that will let me track the statistics of a certain user on the game League of Legends using the Riot Games API. I have made a method that will allow me to parse the number of wins and losses from the servers and calculate the ratio between them, but I need to call it from inside onCreate, or any other method that I'll be able to call from onCreate.

Here is the method :

public static void checkStats(String[] args) throws RiotApiException {
    RiotApi api = new RiotApi("DEVELOPER KEY REDACTED");
    Summoner summoner = api.getSummonerById(Region.EUW, "ID REDACTED");
    RankedStats statsRanked = api.getRankedStats(Region.EUW, summoner.getId());
    AggregatedStats rankedStats = statsRanked.getChampions().get(0).getStats();
    wins = rankedStats.getTotalSessionsWon();
    losses = rankedStats.getTotalSessionsLost();
    ratio = wins / losses;

    ratioView.setText(wins + " / " + losses + " | " + "Ratio : " + ratio);
}

wins, losses, ratio and ratioView are all defined above in the code :

static int points, wins, losses, ratio, BOtarget, BOwins, BOlosses, BOprogress;
static TextView ratioView;

I am trying to call this method by using this line, but it says checkstats(String[]) cannot be applied to checkstats() :

checkStats();

So I tried to use :

checkstats(null);

But now, I get an Unhandled exception :

Unhandled exception: net.rithms.RiotApiException

All imports related to the API are correctly done.

For reference, I am following this example : https://github.com/taycaldwell/riot-api-java/blob/master/examples/RankedWinsAndLosses.java

Thank you for your help.

EDIT : entire activity code : http://pastebin.com/2f65WTWN

  • You do not need make them `static`. What you need is `AsyncTask` to make API call behind of main thread. I am not sure whether `Riot` handles async. or not. – adnbsr Feb 15 '17 at 16:24
  • Hello, I tried to do this : https://gyazo.com/1cac084a4fcd5c651136fc0475dd7722 but now I don't know how to call a class from onCreate. Any thoughts on the code / help on calling a class from onCreate ? – Arnaud Lesueur Feb 15 '17 at 20:26

1 Answers1

0

The onCreate method in Android Java is not static (compared to the main method in a Java program).

Thus, in your Activity, you do not need to make the int points, wins, losses, ratio, BOtarget, BOwins, BOlosses, BOprogress; or TextView ratioView; variables static either. Simply define them as class level variables in your Activity.

Simply define your method like this, and call it from onCreate

public void checkStats() throws RiotApiException {
    ...

When you call checkStats(), you need to wrap it in a try catch like this:

try {
    checkStats();
} catch (RiotApiException e) {
    // Handle the exception
    Log.e("NewLayoutActivity", e.getMessage());
}

For what it's worth, the example you're following is just in Java, not on the Android platform, so you'll have to make some adjustments to the code.

Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
  • If I don't make ints and TextViews static, I get an error when I try to use them in the statc method : Non-static field 'whatever' cannot be referenced from a static context. Also, if I just remove the String[] args part and try to call it, I get the RiotApiException. – Arnaud Lesueur Feb 15 '17 at 16:28
  • Which of your other methods are static? If you're just calling these methods from your Activity they don't need to be static. – Andrew Brooke Feb 15 '17 at 16:30
  • Right now, this is the only static method in the code. I tried to replace public static void with public void and to declare ints and textviews as private but I get the same exception. – Arnaud Lesueur Feb 15 '17 at 17:37
  • Can you just post your entire Activity's code in your question? – Andrew Brooke Feb 15 '17 at 17:42
  • I added it as a pastebin link, since I couldn't get it to show properly with the code format on this site. http://pastebin.com/2f65WTWN By the way, I'm using a tabbed activity with 3 fragments. – Arnaud Lesueur Feb 15 '17 at 17:51
  • What is the exact error you're getting? You need to wrap the call to `checkStats` in a `try catch` to handle the `RiotApiException` – Andrew Brooke Feb 15 '17 at 18:28
  • This is the exact error message I get if I try to compile the code as it is : http://i.imgur.com/VOpJQu2.png How do I build a try catch ? I tried to do it like this : https://gyazo.com/3da707d7889bedaeb782dc49e81f643c and now the code compiles, but the application crashes on launch with the error "java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.folder.appname/com.example.folder.appname.NewLayoutActivity}: android.os.NetworkOnMainThreadException" – Arnaud Lesueur Feb 15 '17 at 18:37
  • Okay, that's an entirely new question, and probably a bit out of scope for what your original issue was. I recommend creating a new question for that problem, separate from this one. You may want to look into this though: http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception – Andrew Brooke Feb 15 '17 at 18:41
  • Alright, thanks a lot for your help, you technically resolved my issue even though the fix created a new one ^^ I'll try to fix the new error and make a thread if I can't. – Arnaud Lesueur Feb 15 '17 at 18:55