这里我试图在XML和JAVA中使用Spinner。具体来说,我做了一个程序,显示关于茶,风格,flava,顶部选择的Toast。在进行此步骤之前,我确实检查了该程序的另一个更简单版本,此时一切运行顺利。然而,它是如此令人困惑。事实上,我没有经验足以找出问题所在。请帮助一个新手,代码可能会有点长,我会非常,非常感谢
package com.example.second_spinner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class Second_Spinner extends Activity {
private String tea_message = "";
private String style_message = "";
private String flava_message = "";
private String moreLove_message = "";
private String message = getString(R.string.message);
private int maximum_flava = 4;
private ArrayList<String> alreadyFlava = new ArrayList<String>();
private ArrayList<String> alreadyLove = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);;
setContentView(R.layout.activity_second__spinner);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner1.setOnItemSelectedListener(new MilkTea());
spinner2.setOnItemSelectedListener(new MilkTea());
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
Spinner spinner4 = (Spinner) findViewById(R.id.spinner4);
List<String> flava = getFlava();
ArrayAdapter<String> spinnerAdapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,flava);
spinnerAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(spinnerAdapter1);
spinner3.setOnItemSelectedListener(new MilkTea());
List<String> moreLove = getMoreLove();
ArrayAdapter<String> spinnerAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,moreLove);
spinnerAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(spinnerAdapter2);
spinner4.setOnItemSelectedListener(new MilkTea());
}
private class MilkTea implements OnItemSelectedListener {
private boolean isFirst;
public void onItemSelected(AdapterView<?> spinner, View selectedView, int selectedIndex, long id) {
if (isFirst == false) {
isFirst = true;
}
else {
switch (selectedView.getId()) {
case (R.id.spinner1) :
tea_message = spinner.getItemAtPosition(selectedIndex).toString();
break;
case (R.id.spinner2) :
style_message = spinner.getItemAtPosition(selectedIndex).toString();
break;
case (R.id.spinner3) :
if (maximum_flava > 0) {
if (alreadyFlava.isEmpty()) {
flava_message = spinner.getItemAtPosition(selectedIndex).toString();
alreadyFlava.add(flava_message);
maximum_flava--;
}
else {
boolean sign = true;
String temp = spinner.getItemAtPosition(selectedIndex).toString();
for (int i = 0; i < alreadyFlava.size(); i++)
if (temp.equals(alreadyFlava.get(i))) {
sign = false;
break;
}
if (sign == true) {
flava_message += ","+temp;
alreadyFlava.add(flava_message);
maximum_flava--;
}
}
}
break;
case (R.id.spinner4) :
if (alreadyLove.isEmpty()) {
moreLove_message = spinner.getItemAtPosition(selectedIndex).toString();
alreadyFlava.add(flava_message);
}
else {
boolean sign = true;
String temp = spinner.getItemAtPosition(selectedIndex).toString();
for (int i = 0; i < alreadyLove.size(); i++)
if (temp.equals(alreadyLove.get(i))) {
sign = false;
break;
}
if (sign == true) {
moreLove_message += ","+temp;
alreadyLove.add(moreLove_message);
}
}
break;
}
message = String.format(message, tea_message, style_message, flava_message, moreLove_message);
Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> spinner) {
}
}
private List<String> getFlava() {
String[] flava = {
"No, I'm good", "Honeydew", "Blood Orange", "Red Guava", "Kumquat", "Lychee", "Longan", "Blackberry",
"Pineapple", "Mango", "Passion Fruit", "Taro", "Strawberry", "Coconut"
};
List<String> flava_list = Arrays.asList(flava);
return flava_list;
}
private List<String> getMoreLove() {
String[] moreLove = {
"Not Today","Aloe Vera","Black Pearls", "Coffee Jelly","White Pearls","Grass Jelly","Passion Jelly",
"Coconut Jelly","Lychee Jelly","Strawberry Jelly","Manga Jelly","Manga Stars"
};
List<String> moreLove_list = Arrays.asList(moreLove);
return moreLove_list;
}
}
答案 0 :(得分:1)
private String message = getString(R.string.message);
您过早地访问资源。您无法在活动构建时访问资源,例如初始化成员变量时。您需要等到活动生命周期中的onCreate()
。将getString()
移到那里。
通常,要了解出错的地方,请参阅logcat中的异常堆栈跟踪。也包括在你的问题中。见Unfortunately MyApp has stopped. How can I solve this?