嗨,我正在学习Android Spinner ..在我的教程中我有两个Spinner国家和城市,基于国家选择城市将改变其工作,但当我能够将选定的国家和城市传递给新的活动我得到一个错误请帮帮我
答案 0 :(得分:1)
错误是您在secondActivy中传递了Firstactivity布局名称,这是导致应用崩溃的原因
答案 1 :(得分:0)
您应该使用bundle将多个数据发送到另一个活动,而不是intent.putExtra()
例如: -
// In your First Activity
public void send() {
Intent intent= new Intent(this,Display_Spinner.class);
Bundle bundle = new Bundle();
bundle.putString("COUNTRY", str_country);
bundle.putString("CITY",str_city);
intent.putExtras(bundle);
startActivity(intent);
}
// In your second Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// In your second Activity fetch data like this
Bundle bundle = getIntent().getExtras();
country= (TextView)findViewById(R.id.textView3);
city=(TextView)findViewById(R.id.textView5);
String Counrty_name=bundle.getString("COUNTRY");
String City_name=bundle.getString("CITY");
country.setText(Counrty_name);
city.setText(City_name);
}
}
更新3:
在您的第一个活动的onCreate中使用此代码
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
str_country=country.getSelectedItem().toString();
if (position == 0) {
adapter = ArrayAdapter.createFromResource(this, R.array.city_select, android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
if (position == 1) {
adapter = ArrayAdapter.createFromResource(this, R.array.city_india, android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
if (position == 2) {
adapter = ArrayAdapter.createFromResource(this, R.array.city_pakisthan, android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
if (position == 3) {
adapter = ArrayAdapter.createFromResource(this, R.array.city_srilanka, android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
str_city=city.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 2 :(得分:0)
在第二项活动中试试。
$Array2 = array
(
"fooo" =>array(
"bla " => $Array1
)
)
答案 3 :(得分:0)
似乎代码是正确的,但只缺少一个测试用例。当用户选择某个国家/地区时,您可以为城市设置其适配器,同时确保为城市选择一些默认值。由于按下按钮时没有为城市选择的值,因为在行String city=sp2.getSelectedItem().toString()
上调用空引用的toString(),应用程序崩溃。这里你应该做的很简单:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
adapter= ArrayAdapter.createFromResource(this,R.array.city_select,android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
if (position==1){
adapter= ArrayAdapter.createFromResource(this,R.array.city_india,android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
if (position==2){
adapter= ArrayAdapter.createFromResource(this,R.array.city_pakisthan,android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
if (position==3){
adapter= ArrayAdapter.createFromResource(this,R.array.city_srilanka,android.R.layout.simple_spinner_item);
sp2.setAdapter(adapter);
}
//write this:
sp2.setSelection(0);//by default first city is selected
}