App crashes after 2nd chirp

Solved!
Posted in Android by Christos Tsiotsias Fri Mar 24 2017 13:34:41 GMT+0000 (UTC)·4·Viewed 171 times

Hi all, I am trying to create a basic test app on Android following your documentation. All I'm trying to do is play a chirp when a button is pressed and let another device which is running the same app listen to that chirp and output its data. My problems are 2: Firstly my device does not listen to any chirps. Secondly, the chirp plays normally the first time I press my button but once I press it for 2nd time, the chirp plays and then my app crashes. Inside OnCreate method I am initialising the ChirpSDK object, setting the Listeners on it and also doing the same thing for my button. This is the code inside OnCreate: chirpSDK = new ChirpSDK(this, my API_KEY, my API_SECRET); chirpSDK.setListener(new ChirpSDKListener() { @Override public void onChirpHeard(Chirp chirp) { readChirp(chirp); } @Override public void onChirpHearStarted() { } @Override public void onChirpHearFailed() { } @Override public void onChirpError(ChirpError chirpError) { } }); button = (Button) findViewById(R.id.clickButton); button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick (View v) { chirpSDK.start(); chirpSDK.chirp(new Chirp("5lhlfc0kfe")); button.setText("clicked"); } }); and that's the readChirp method: private void readChirp(Chirp chirp) { chirpSDK.read(chirp, new CallbackRead() { @Override public void onReadResponse(Chirp chirp) { String x = chirp.getChirpData().toString(); button.setText("read"); } @Override public void onReadError(ChirpError chirpError) { } }); } Any help appreciated. Thanks.
Mike Smales
Mar 25, 2017

Hi Christos,



Thanks for getting in touch.

You are experiencing the crash as you are calling chirpSDK.start() multiple times, without calling chirpSDK.stop(). This causes an IllegalStateException to be thrown with the message "start called when already started". 



Looking at your code, I would recommend moving the call to chirpSDK.start() to outside of the button onClickListener(). 



This is also most likely the reason why you are not hearing any chirps as chirpSDK.start() needs to be called before chirps will be heard.

Let me know if that doesn't fix the issue.

Mike


Mike Smales marked this as solved
Christos Tsiotsias
Mar 27, 2017

Hi Mike,

Thank you for the quick response.

As you said, I placed the chrirpSDK.Start() call inside onCreate but outside button's listener. So now it is just called once when the program starts, while I am not calling the stop() method at all. Indeed I can now produce sound without my app crashing and can also read a chirp!

I have only one more question and that would be how to decode a Chirp object. I have posted a json line from the api, something like { "name": "John" } and got a sortcode. I have managed to read and output the sortcode by using getIdentifier() but I am not sure how I can get the key - value strings. I tried using:

JSONObject json = chirp.getJsonData();
String name = json.getString("name");

but that crashes my app. What am I doing wrong ? :/

Thank you in advance.
Christos

Mike Smales
Mar 29, 2017

Hi Christos,

If the app is crashing on that line, it would suggest the json object is null. Can you confirm with your debugging?

Mike

Markdown is allowed