我有一个RecyclerView
,点击后会显示存储在Database
中的多个图片路径。单击第1行直到最后一行时,它会在TextView
中显示多个图像路径。但我的问题是,当点击第0行时,应用程序崩溃。
这是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flip);
txtLoc = (TextView) findViewById(R.id.samp);
Intent goFlip = getIntent();
Bundle bundle = goFlip.getExtras();
String getLocation = bundle.getString("path");
index =bundle.getInt("pos");
db = new DatabaseHandler(this);
dbList = new ArrayList<GettersSetters>();
dbList = db.searchFromDB(getLocation);
String locate = "";
if(dbList.size() > 0 ){
for(GettersSetters currentClass : dbList){
locate +=dbList.get(index-1).getPath() + ",";
Log.w("RESULT PATHS", locate);
}
}
txtLoc.setText(locate);
}
它产生以下错误。
09-09 17:33:39.141 24147-24147/com.luminous.pick E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.luminous.pick/com.luminous.pick.FlipActivity}: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
at java.util.ArrayList.get(ArrayList.java:306)
at com.luminous.pick.FlipActivity.onCreate(FlipActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
答案 0 :(得分:2)
全局声明索引
int index=0;
并使用此方法代替您的....
for(GettersSetters currentClass : dbList){
locate +=dbList.get(index++).getPath() + ",";
Log.w("RESULT PATHS", locate);
}
答案 1 :(得分:0)
您不必从索引中减去1,因为如果索引等于0,则得到-1。