2017-09-11 222 views
-5

在我的小程序,我想顯示從我String.xml 東西我normaly使用它來做到這一點:java.lang.ArrayIndexOutOfBoundsException:length = 6; index = 6 | String.xml

public void putText() { 
    String[] text1 = getResources().getStringArray(R.array.text1); 
    String[] text2 = getResources().getStringArray(R.array.text2); 


    if(selecter >= MAX) { 
     selecter = 0; 
    } 
    else { 
     selecter++; 
    } 

    // Texte anzeigen und Shown table auf true setzen. 
    // normally selecter instead of 6 
    txt_text1.setText(text1[6]); 
    txt_text2.setText(text2[6]); 
} 

而是「selecter」時是6至9之間(九更是MAX變量)的應用程序崩潰與此錯誤:

FATAL EXCEPTION: main 
                Process: com.example.alex.wouldyoupressthebutton, PID: 22844 
                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alex.wouldyoupressthebutton/com.example.alex.wouldyoupressthebutton.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2750) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811) 
                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                 at android.os.Looper.loop(Looper.java:154) 
                 at android.app.ActivityThread.main(ActivityThread.java:6316) 
                 at java.lang.reflect.Method.invoke(Native Method) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
                Caused by: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6 
                 at com.example.alex.wouldyoupressthebutton.MainActivity.putText(MainActivity.java:175) 
                 at com.example.alex.wouldyoupressthebutton.MainActivity.onCreate(MainActivity.java:85) 
                 at android.app.Activity.performCreate(Activity.java:6757) 
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)  
                 at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)  
                 at android.os.Handler.dispatchMessage(Handler.java:102)  
                 at android.os.Looper.loop(Looper.java:154)  
                 at android.app.ActivityThread.main(ActivityThread.java:6316)  
                 at java.lang.reflect.Method.invoke(Native Method)  
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)  
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)  

這裏也是我的string.xml

<string-array name="text1"> 
    <item>You would have everything you want</item> 
    <item>You can go back in time when you want</item> 
    <item>You gain the ability to communicate with animals</item> 
    <item>You gain any one super-power of your choice (aside from immortality)</item> 
    <item>You would become the greatest player in your favorite sport</item> 
    <item>You gain omniscience (infinite knowledge of everything)</item> 
    <item>You become the most talented musician in the world (any instrument)</item> 
    <item>Instead of Dying, you regenerate</item> 
    <item>You wil have unlimited money</item> 
    <item>Everyone on earth love and adore you</item> 
</string-array> 
<string-array name="text2"> 
    <item>you don\'t understand the human language anymore.</item> 
    <item>you can only talk to one person per each travel.</item> 
    <item>you lose the ability to communicate with humans</item> 
    <item>you lose your ability to swim</item> 
    <item>you would constantly be criticized</item> 
    <item>you have it for 24 hours, and afterwards you gain permanent down syndrome.</item> 
    <item>you cannot earn any money from your talent.</item> 
    <item>you only can regenerate 12 times, and each time you look like a different person but retain all memories.</item> 
    <item>you can never sleep again.</item> 
    <item>never fall in love yourself.</item> 
</string-array> 

我在String.x嘗試了短文本毫升,但我仍然得到了消息...而這隻發生在六和MAX(9)

任何建議,爲什麼?

回答

0

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6

意味着你正在試圖在數組的索引6長度爲6到接入元件,但陣列是基於0的索引,這意味着第一元件處於不0位置1。因此,一個6個元素的數組中的最大指數是指數5

你的錯,可能是在這一行:

if(selecter >= MAX) 

應該

if(selecter > MAX) 

但是你不顯示什麼是MAX值。它不能> 6。在你的代碼中的某處,你應該將其定義爲int MAX = 6;

+0

事情是在string.xml中有9個條目,所以我必須從0開始,到9,爲什麼它說只有6 – MrMinemeet

+0

是的,我觀察到,你有沒有讓你的應用程序看看數組'text1'和'text2'的真實內容是什麼?一個簡單的'Log.d(「TAG」,text1.length)''會顯示出陣列的真實大小 – pleft

+0

當我在虛擬設備上試用它時,它說它的長度爲10,它的工作原理是** selecter = 9 * * ...但是當我在我的LG G6或任何其他真實設備上嘗試它時,它會崩潰,並說它只有6長。 – MrMinemeet

-1

你可以增加MAX的值。 也許MAX = MAX + 1;

+0

不做任何事 – MrMinemeet