2016-09-09 13 views
-2

RecyclerViewをクリックすると、Databaseに保存されている複数の画像パスが表示されます。行1を最後の行までクリックすると、複数の画像パスがTextViewに表示されます。しかし、私の問題は、行0をクリックするとアプリケーションがクラッシュすることです。java.lang.ArrayIndexOutOfBoundsExceptionのデバッグ方法:length = 12; index = -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 ur stacktraceは、問題はあなたがインデックス '-1'を呼んでいるということです。私はget(index-1)呼び出しの可能性を見ています。 – xenteros

+0

'' 'find + = dbList.get(index-1).getPath()+"、 ";' ''ここにこの行にエラーがあります。 index = 0の場合-1を返します。 –

+0

@xenteros -1を削除しようとしていますが、まだエラーが発生しています –

答えて

2

宣言指数グローバル

int index=0; 

、代わりにあなたのこのメソッドを使用し....

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

ありがとう、それは –

+0

あなたを歓迎しています.... – sushildlh

0

あなたはインデックスから1を減算する必要はありませんので、 indexが0の場合は-1を返します。

関連する問題