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)
对于这个不起眼的问题感到抱歉。我是初学者程序员,遇到了另一个例外。我试图搞乱我的ArrayList代码和方法.get()但无济于事我再次来到这里。当我点击我的按钮'计算'时会发生这种情况。任何理论?
答案 0 :(得分: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 :(得分:3)
当您调用new ArrayList(4)时,您将设置列表的初始容量,而不是其大小。
所以当你致电:
Evaluator.get(0)
你正在打电话给空列表。
您必须在列表中添加项目或在调用get()之前检查列表是否包含项目。
添加项目
Evaluator.add(""); \\times 4
检查尺寸:
if(Evaluator.size()>0)
{
//your code here
}
答案 2 :(得分:1)
我简要介绍了一下你的代码:
您的Evaluator
是ArrayList
的{{1}}。这意味着它的大小可能会在运行时动态变化。在String
方法中,您使用onItemSelected
方法,这会减少remove()
的大小。假设您的ArrayList
只有3个ArrayList
:您无法调用String
,因为这会指向您没有的第4个元素。
以下是一些可以运行和理解的纯Java代码:
import java.util.ArrayList;
Evaluator.get(3)
它将提供以下输出:
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)
删除节点后,它的大小将从3降至2.因此,您无法通过调用ArrayList
来访问第3个元素。
答案 3 :(得分:0)
我认为您的问题出在
中type = new String[3];
应该是
type = new String[4]
这是因为数组从0开始而不是1.我看到你正在尝试向数组添加4个项目。