我的代码有问题,当我尝试做一个Intent应用程序崩溃而我不知道为什么,我已经检查了我的清单并没有问题,这是我的代码
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);
}
});
这是我的清单:
<?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>
这是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
这是我的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);
}
}
}
提前致谢
答案 0 :(得分:0)
第61行:
int horaDelCole = HoraCole[i+1]*60 + MinutosCole[i+1];
您正在尝试访问超出数组大小的元素。
[i+1]
使用[i + 1]是一个有效的索引,直到i == 47,然后你试图抓取元素48.如果你试图迭代数组中的所有元素,你可以这样做:
int horaDelCole = HoraCole[i]*60 + MinutosCole[i];