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?
您應該已經學會了如何從最後一個問題中讀取堆棧跟蹤。 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
我明白問題出在哪裏,我只是想知道如何解決問題!第136行是空格 –
好的,這是越來越討厭,堆棧跟蹤將永遠不會引用空行。與上次一樣 - 使用匹配的堆棧跟蹤發佈實際代碼。 – Simon