2016-09-09 50 views
-2

我有一個RecyclerView,當點擊它將顯示多個圖像路徑存儲在Database。當點擊行1直到最後一行時,它將顯示TextView中的多個圖像路徑。但我的問題是,當點擊第0行時,應用程序崩潰。如何調試java.lang.ArrayIndexOutOfBoundsException:length = 12;索引= -1

這裏是我的代碼

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

Yo你的stacktrace說問題在於你正在調用索引'-1'。我看到你的get(index-1)調用有潛力。 – xenteros

+0

'''locate + = dbList.get(index-1).getPath()+「,」;「'''這裏是這行有錯誤。當索引= 0時索引-1將返回-1 –

+0

@xenteros我嘗試刪除-1但仍Im有錯誤 –

回答

2

申報索引全球

int index=0; 

,並使用此方法,而不是你的....

for(GettersSetters currentClass : dbList){ 
       locate +=dbList.get(index++).getPath() + ","; 
       Log.w("RESULT PATHS", locate); 
      } 
+0

謝謝,它的作品 –

+0

歡迎.... – sushildlh

0

您不必從指數減去1,因爲如果指數等於0,則獲得-1。

相關問題