Question

i have a problem with my code, when I try to do a Intent the app crash and I don't know why, I've already checked my manifest and no problem, here is my code

img = (ImageView) findViewById(R.id.imageView1);

    img.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(MainActivity.this, nextCole.class);
            MainActivity.this.startActivity(myIntent);
        }
    });

this is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.germankatz.etacertime"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.germankatz.etacertime.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".nextCole"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.ALTERNATIVE" />
        </intent-filter>
    </activity>
</application>

</manifest>

This is the logcat

05-01 15:57:11.099: D/AndroidRuntime(19583): Shutting down VM
05-01 15:57:11.099: W/dalvikvm(19583): threadid=1: thread exiting with uncaught exception (group=0x416a5d40)
05-01 15:57:11.101: E/AndroidRuntime(19583): FATAL EXCEPTION: main
05-01 15:57:11.101: E/AndroidRuntime(19583): Process: com.germankatz.etacertime, PID: 19583
05-01 15:57:11.101: E/AndroidRuntime(19583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.germankatz.etacertime/com.germankatz.etacertime.nextCole}: java.lang.ArrayIndexOutOfBoundsException: length=48; index=48
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.os.Looper.loop(Looper.java:136)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at java.lang.reflect.Method.invoke(Method.java:515)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at dalvik.system.NativeStart.main(Native Method)
05-01 15:57:11.101: E/AndroidRuntime(19583): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=48; index=48
05-01 15:57:11.101: E/AndroidRuntime(19583):    at com.germankatz.etacertime.nextCole.onCreate(nextCole.java:61)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.Activity.performCreate(Activity.java:5248)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
05-01 15:57:11.101: E/AndroidRuntime(19583):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
05-01 15:57:11.101: E/AndroidRuntime(19583):    ... 11 more

this is my nextCole.class

 package com.germankatz.etacertime;
 import java.util.Calendar;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.Window;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.germankatz.etacertime.R;

 public class nextCole extends Activity{

TextView tv1, tv2, tv3;
String cuandoSale;
long resultadoFinal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.next_cole);


    // Declaro los horarios

    int[] HoraCole = {     00, 1, 4, 5, 5,6, 6, 6, 7, 7, 8, 8, 9, 9, 9,10,10,10,11,11,12,12,12,13,13,13,14,14,15,15,16,16,16,17,17,17,18,18,19,19,19,20,20,20,21,22,22,23};        
    int[] MinutosCole ={   20,30,30,25,40,0,40,50,20,50,00,30,00,10,40,10,20,50,20,30,00,30,40,00,40,50,20,50,00,30,00,10,40,10,20,50,20,30,00,30,40,10,40,50,20,00,30,10};

    Calendar c = Calendar.getInstance(); 
    int minutos = c.get(Calendar.MINUTE);
    int hora = c.get(Calendar.HOUR_OF_DAY);

    tv1 = (TextView) findViewById(R.id.textView1);
    tv2 = (TextView) findViewById(R.id.textView2);
    tv3 = (TextView) findViewById(R.id.textView3);

    tv1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(),"Creado por Germán Katzenelson",Toast.LENGTH_SHORT).show();
        }
    });

    // Valor maximo que cualquier valor es menor
    int valorPosta = 999999999;

 // Tenemos la hora actual en minutos
    int horaActualEnMin = hora*60 + minutos; 

    if (MinutosCole.length == HoraCole.length){        
        for (int i = 0; i < HoraCole.length; i++){          
            if (HoraCole[i] >= hora){                   

                // Sacamos la hora del colectivo
                int horaDelCole = HoraCole[i+1]*60 + MinutosCole[i+1];

                if (horaActualEnMin < horaDelCole){
                    if(horaDelCole < valorPosta){
                        valorPosta = horaDelCole;
                    }
                }


            }
        };
        int hours = valorPosta / 60; 
        int minutes = valorPosta % 60;

        if (hora == 23 && minutos >= 11){
            tv3.setText("+");
            tv2.setText("00:20");
        }

        else if ((valorPosta - horaActualEnMin) > 60){
            horaActualEnMin = hora*60 + minutos;
            int cuantoQueda = horaActualEnMin - valorPosta;

            int queda = cuantoQueda % 60;
            if (queda < 0){
                queda = queda * -1;
            }
            tv2.setText(String.valueOf(hours) + ":" + String.valueOf(minutes));
            tv3.setText("+60");
        }

        else{
            horaActualEnMin = hora*60 + minutos;
            int cuantoQueda = horaActualEnMin - valorPosta;

            int queda = cuantoQueda % 60;
            if (queda < 0){
                queda = queda * -1;
            }

            tv2.setText(String.valueOf(hours) + ":" + String.valueOf(minutes));
            tv3.setText(String.valueOf(queda));
        }
    }
    else{
        tv2.setText("Pelotudo no cordinaste las fechas " + HoraCole.length + " faowefoa: " + MinutosCole.length);
    }

}

   }

thanks in advance

Was it helpful?

Solution

On line 61:

int horaDelCole = HoraCole[i+1]*60 + MinutosCole[i+1];

You are attempting to access an element one outside the size of your array.

[i+1]

Using [i+1] is a valid index until i == 47, then you are attempting to grab element 48. If you are trying to iterate over all of the elements in the array you can do:

int horaDelCole = HoraCole[i]*60 + MinutosCole[i];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top