java - How to pass string in android without error- A resource was acquired at attached stack trace but never released- -


I try to bring the stringbuilder object to my main activity. I check my JSON file or the parsing code that they work well, however, when I try to bring a stringbilder, it gave an error: A resource was acquired on the attached stack trace, But it was never released. For information about avoiding resource leaks see java.io.closeable. Code for server.java is given below

Enhances {static stringbillder stringbilder = new stringbilder (); Public server () {try {JSONObject obj = new JSONObject (loadJSONFromAsset ()); JSONArray Project = obj.getJSONArray ("Project"); For (int i = 0; i

And here is my main activity Hakjawa

  public class extends the main activity (Text View jsonDataTextView; @ Override protected create null ( Bundle saved Instansstet) saved {Suprknkret (Instensstet); setContentView (R.layout.activity_main); JsonDataTextView = (TextView) searches ViewById (R.id.textView); server = new server (); JsonDataTextViewksetText ( S.stringbuilder.toString ());}}  

Is there a solution?

InputStream to your Try de> loadJSONFromAsset method can not be closed block, and it should

  public string load jsonfromaset () {string json = null; InputStream = null; try {is = getAssets (). Open ( "cat.json"); Int size = is.available (); byte [] buffer = new byte [size]; is.read (buffer) ; Json = new string (buffer, "UTF-8");} hold (IOException pre) {ex.printStackTrace (); } Finally {if (is! = Null) {try {is.close ()}; } Hold (IOException ex) {// Do you want to handle this exception? }}} Return json; }  

Note: In some servers constructor, I get bored in the hold block, you can change your error in the "Code> StringBuilder < / Code>. Do you know that stringbuilder can not be blank here.

Note: As a server: In the block block, To add some strings, one or more attempts (or) may work at some point in. A non-activity

  public class server {private concurrent RT mContext; public server (References) {mContext = context; ...} ... public string load JSONFromAsset () {... mContext.getAssets (). Open ( "cat.json");}}  

then in your main activity

  server = new server (this);  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -