2013-11-25 137 views
0
package org.example.mbtiapplication; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class MBTITest extends Activity implements OnItemSelectedListener { 

    int I, E, N, S, T, F, J, P; 
    String[] type; 
    ArrayList<String> Evaluator = new ArrayList<String>(4); 
    Spinner firstSpinner ; 
    Spinner secondSpinner ; 
    Spinner thirdSpinner; 
    Spinner fourthSpinner ; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_mbtitest); 
     firstSpinner = (Spinner) findViewById(R.id.spinner1); 
     secondSpinner = (Spinner) findViewById(R.id.spinner2); 
     thirdSpinner = (Spinner) findViewById(R.id.spinner3); 
     fourthSpinner = (Spinner) findViewById(R.id.spinner4); 
     type = new String[3]; 
     addItemsOnSpinner1(); 
     addItemsOnSpinner2(); 
     addItemsOnSpinner3(); 
     addItemsOnSpinner4(); 
     final Button calculate = (Button) findViewById(R.id.calculate); 
     calculate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       getPersonality(); 
       evaluateType(); 
       TextView calculatedPersonality = (TextView) findViewById(R.id.personalitytype); 
       calculatedPersonality.setText(type.toString()); 

      } 
     }); 


     } 

    public void addItemsOnSpinner1(){ 
     Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1); 
     // Create an ArrayAdapter using the string array and a default spinner layout 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinnerarraybool, android.R.layout.simple_spinner_item); 
     // Specify the layout to use when the list of choices appears 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     // Apply the adapter to the spinner 
     firstSpinner.setAdapter(adapter); 


    } 

    public void addItemsOnSpinner2(){ 
     Spinner firstSpinner = (Spinner) findViewById(R.id.spinner2); 
     // Create an ArrayAdapter using the string array and a default spinner layout 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinnerarraybool, android.R.layout.simple_spinner_item); 
     // Specify the layout to use when the list of choices appears 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     // Apply the adapter to the spinner 
     firstSpinner.setAdapter(adapter); 
    } 

    public void addItemsOnSpinner3(){ 
     Spinner firstSpinner = (Spinner) findViewById(R.id.spinner3); 
     // Create an ArrayAdapter using the string array and a default spinner layout 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinnerarraybool, android.R.layout.simple_spinner_item); 
     // Specify the layout to use when the list of choices appears 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     // Apply the adapter to the spinner 
     firstSpinner.setAdapter(adapter); 
    } 

    public void addItemsOnSpinner4(){ 
     Spinner firstSpinner = (Spinner) findViewById(R.id.spinner4); 
     // Create an ArrayAdapter using the string array and a default spinner layout 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinnerarraybool, android.R.layout.simple_spinner_item); 
     // Specify the layout to use when the list of choices appears 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     // Apply the adapter to the spinner 
     firstSpinner.setAdapter(adapter); 
    } 



    @Override 
    public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, 
      long arg3) { 
     switch (arg1.getId()){ 
     case R.id.spinner1: 
      String firstAnswer = firstSpinner.getSelectedItem().toString(); 
      Evaluator.remove(0); 
      Evaluator.add(0 ,firstAnswer); 
      break; 
     case R.id.spinner2: 
      String secondAnswer = firstSpinner.getSelectedItem().toString(); 
      Evaluator.remove(1); 
      Evaluator.add(1, secondAnswer); 
      break; 
     case R.id.spinner3: 
      String thirdAnswer = firstSpinner.getSelectedItem().toString(); 
      Evaluator.remove(2); 
      Evaluator.add(2, thirdAnswer); 
      break; 
     case R.id.spinner4: 
      String fourthAnswer = firstSpinner.getSelectedItem().toString(); 
      Evaluator.remove(3); 
      Evaluator.add(3, fourthAnswer); 
      break; 
     } 


    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 


    //Evaluates answers 
    public void getPersonality(){ 
     if (Evaluator.get(0) == "True"){ 
      E += 1; 
     } 
     if (Evaluator.get(0) == "In the Middle"){ 
      E += 1; 
      I += 1; 
     } 
     if (Evaluator.get(0) == "False"){ 
      I += 1; 
     } 


     if (Evaluator.get(1) == "True"){ 
      N += 1; 
     } 
     if (Evaluator.get(1) == "In the Middle"){ 
      N += 1; 
      S += 1; 
     } 
     if (Evaluator.get(1) == "False"){ 
      S += 1; 
     } 


     if (Evaluator.get(2) == "True"){ 
      F += 1; 
     } 
     if (Evaluator.get(2) == "In the Middle"){ 
      F += 1; 
      T += 1; 
     } 
     if (Evaluator.get(2) == "False"){ 
      T += 1; 
     } 


     if (Evaluator.get(3) == "True"){ 
      P += 1; 
     } 
     if (Evaluator.get(3) == "In the Middle"){ 
      P += 1; 
      J += 1; 
     } 
     if (Evaluator.get(3) == "False"){ 
      J += 1; 
     } 

    } 

    public void evaluateType(){ 
     //INTRO VS EXTRO 
     if (E > I){ 
      type[0] = "E"; 
     } 
     if (I > E){ 
      type[0] = "I"; 
     } 
     if (I == E){ 
      type[0] = "x";  
     } 
     //INTUITION VS SENSING 
     if (N > S){ 
      type[1] = "N"; 
     } 
     if (S > N){ 
      type[1] = "S"; 
     } 
     if (S == N){ 
      type[1] = "x";  
     } 
     //THINKING VS FEELING 
     if (T > F){ 
      type[2] = "T"; 
     } 
     if (F > T){ 
      type[2] = "F"; 
     } 
     if (T == F){ 
      type[2] = "x";  
     } 
     //JUDGING VS PERCIEVING 
     if (J > P){ 
      type[3] = "J"; 
     } 
     if (P > J){ 
      type[3] = "P"; 
     } 
     if (P == J){ 
      type[3] = "x";  
     } 

    } 











} 

11-25 16:55:22.154: E/AndroidRuntime(1840): FATAL EXCEPTION: main 
11-25 16:55:22.154: E/AndroidRuntime(1840): Process: org.example.mbtiapplication, PID: 1840 
11-25 16:55:22.154: E/AndroidRuntime(1840): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at java.util.ArrayList.get(ArrayList.java:308) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at org.example.mbtiapplication.MBTITest.getPersonality(MBTITest.java:136) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at org.example.mbtiapplication.MBTITest$1.onClick(MBTITest.java:43) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at android.view.View.performClick(View.java:4424) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at android.view.View$PerformClick.run(View.java:18383) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at android.os.Handler.handleCallback(Handler.java:733) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at android.os.Handler.dispatchMessage(Handler.java:95) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at android.os.Looper.loop(Looper.java:137) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at java.lang.reflect.Method.invoke(Method.java:515) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
11-25 16:55:22.154: E/AndroidRuntime(1840):  at dalvik.system.NativeStart.main(Native Method) 

對不起nooby問題。我是一名初學者程序員,遇到了另一個例外。我試圖弄亂我的ArrayList代碼和方法.get(),但無濟於事,我再次來到這裏。當我點擊我的按鈕「計算」時會發生這種情況。任何理論?爲什麼我會得到這個IndexOutofBoundsexception?

+1

您應該已經學會了如何從最後一個問題中讀取堆棧跟蹤。 java.lang.IndexOutOfBoundsException:索引0無效,大小爲0 11-25 16:55:22.154:E/AndroidRuntime(1840):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 11-25 16:55:22.154:E/AndroidRuntime(1840):在java.util.ArrayList.get(ArrayList.java:308) 11-25 16:55:22.154:E/AndroidRuntime(1840):at org.example。 mbtiapplication.MBTITest.getPersonality(MBTITest.java:136)'第136行是什麼? – Simon

+0

我明白問題出在哪裏,我只是想知道如何解決問題!第136行是空格 –

+1

好的,這是越來越討厭,堆棧跟蹤將永遠不會引用空行。與上次一樣 - 使用匹配的堆棧跟蹤發佈實際代碼。 – Simon

回答

3

當您調用新的ArrayList(4)時,您將設置列表的初始容量,而不是其大小。

所以,當你撥打:

Evaluator.get(0) 

正在撥打的是空的名單上獲得。

您必須將項目添加到列表中,或者在調用get()之前檢查列表是否包含項目。

添加項目:

Evaluator.add(""); \\times 4 

檢查大小:

if(Evaluator.size()>0) 
{ 
    //your code here 
} 
0

我覺得你的問題是在

type = new String[3]; 

應該

type = new String[4] 

這是因爲數組從0開始,而不是1我看到你試圖添加4個項目到陣列。

+1

那麼索引怎麼是0和大小是0?這表明OP正在引用一個空數組。 – Simon

+0

也許......我會試試看,即使在說明元素中有多少個元素時,我也一直認爲它考慮到了它從0開始。 –

+0

你這樣做,但是異常顯示size = 0。大小是人類可讀的,從1開始,索引從0開始。 – Simon

4
ArrayList<String> Evaluator = new ArrayList<String>(4); 

這不會給你的4項

一個ArrayList試着將

ArrayList<String> Evaluator = new ArrayList<String>(4); 

ArrayList<String> Evaluator = new ArrayList<String>(4); 
Evaluator.add("");Evaluator.add("");Evaluator.add("");Evaluator.add(""); 

你看到現在的問題是什麼?

1

我在你的代碼有一個簡單的介紹一下:

EvaluatorString秒的ArrayList。這意味着它的大小可能在運行時動態變化。在您的onItemSelected方法中,您使用remove()方法,這會減少您的ArrayList的大小。假設你的ArrayList只有3個String s:你不能呼叫,因爲那會指向你沒有的第4個元素。

下面是一些普通的Java代碼可以運行和了解:

進口java.util中。數組列表;

public class Main { 
    public static void main(String[] args) { 
     ArrayList<String> evaluator = new ArrayList<String>(4); 


     evaluator.add("A"); 
     evaluator.add("B"); 
     evaluator.add("C"); 
     evaluator.remove(0); 

     System.out.print(evaluator.get(2)); 
    } 
} 

它會給你以下的輸出:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 
    at java.util.ArrayList.rangeCheck(ArrayList.java:604) 
    at java.util.ArrayList.get(ArrayList.java:382) 
    at Main.main(Main.java:14) 

一旦你從ArrayList刪除節點,它的大小從3降至2.所以,你無法通過調用get(2)訪問第三元素。

+0

在我的ArrayList的.remove()之後立即應用.add()方法到我刪除的相同索引以彌補丟失的空間。 –

+0

是的,的確如此。但爲了將來的參考,你需要知道ArrayList的大小是動態的,所以你不能認爲它會有你想象的大小。 –