Application closing while consuming json

1

What should I do to fix this crash?

My code:

public class Tela1 extends AppCompatActivity {

    TextView textView2;
    String[] objetos = new String[0];
    String url1 = "https://economia.awesomeapi.com.br/json/USD-BRL/1";
    JSONObject jsonObjectTexts;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela1);

        new AsyncTaskExample().execute(url1);

        ImageView myImageView = (ImageView) findViewById(R.id.imageView);
        myImageView.setImageResource(R.drawable.imagem);


    }

    public class AsyncTaskExample extends AsyncTask<String, String, String[]> {

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected String[] doInBackground(String... url) {

            try {
                jsonObjectTexts = JsonParser.readJsonFromUrl(url[0]);
                objetos[0] = jsonObjectTexts.getString("ask");
            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }

            return objetos;
        }

        @Override
        protected void onPostExecute(String[] stringFromDoInBackground) {

            textView2 = (TextView) findViewById(R.id.textView2);


            textView2.setText(stringFromDoInBackground[0]);
            }

    }
}

Error that I receive:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: br.com.planetsweb.dolarhoje, PID: 2917
                  java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                      at br.com.planetsweb.dolarhoje.Tela1$AsyncTaskExample.onPostExecute(Tela1.java:60)
                      at br.com.planetsweb.dolarhoje.Tela1$AsyncTaskExample.onPostExecute(Tela1.java:35)
                      at android.os.AsyncTask.finish(AsyncTask.java:632)
                      at android.os.AsyncTask.access$600(AsyncTask.java:177)
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5045)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                      at dalvik.system.NativeStart.main(Native Method)
Application terminated.
    
asked by anonymous 01.01.2017 / 13:50

1 answer

4

On this line

String[] objetos = new String[0];

You initialize an array of 0 positions. In java arrays does not have dynamic size, it would be interesting to swap this array for an arraylist.

The error occurs because as the array has 0 positions you can not access the first position. This is what is reported in the error message that you received.

    
01.01.2017 / 14:20