我不确定为什么这不起作用。我在 com.example.bertin.HotelLocation.onItemSelected 处收到一个 Log Cat 错误,说明 FATAL EXCEPTION: main java.lang.NullPointer Exception。这是代码。onItemSelected 对我来说似乎是正确的,也许我用错了方法。
package com.example.bertin;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class HotelLocation extends Activity implements OnItemSelectedListener {
Spinner countrySpinner, citySpinner;
private ArrayAdapter<String> countryAdapter;
private ArrayAdapter<String> cityAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotel_location);
Spinner countrySpinner = (Spinner) findViewById(R.id.spinnerHotelCountry);
Spinner citySpinner = (Spinner) findViewById(R.id.spinnerHotelCity);
// Create an ArrayAdapter using the string array and a default spinner
// layout
ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter
.createFromResource(this, R.array.countries,
android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> cityAdapter = ArrayAdapter
.createFromResource(this, R.array.australia,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
countryAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cityAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
countrySpinner.setAdapter(countryAdapter);
citySpinner.setAdapter(cityAdapter);
countrySpinner.setOnItemSelectedListener(this);
// citySpinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String[] citySpinnerArray = null;
String selected = parent.getItemAtPosition(position).toString();
if (selected.equals("Australia")) {
citySpinnerArray = getResources().getStringArray(R.array.australia);
}
citySpinner.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_spinner_item, citySpinnerArray));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hotel_location, menu);
return true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
堆栈跟踪:
09-10 03:42:02.484: E/AndroidRuntime(2160): FATAL EXCEPTION: main
09-10 03:42:02.484: E/AndroidRuntime(2160): java.lang.NullPointerException
09-10 03:42:02.484: E/AndroidRuntime(2160): at com.example.bertin.HotelLocation.onItemSelected(HotelLocation.java:97)
09-10 03:42:02.484: E/AndroidRuntime(2160): at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
09-10 03:42:02.484: E/AndroidRuntime(2160): at android.widget.AdapterView.access$200(AdapterView.java:49)
09-10 03:42:02.484: E/AndroidRuntime(2160): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
09-10 03:42:02.484: E/AndroidRuntime(2160): at android.os.Handler.handleCallback(Handler.java:730)
09-10 03:42:02.484: E/AndroidRuntime(2160): at android.os.Handler.dispatchMessage(Handler.java:92)
09-10 03:42:02.484: E/AndroidRuntime(2160): at android.os.Looper.loop(Looper.java:137)
09-10 03:42:02.484: E/AndroidRuntime(2160): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-10 03:42:02.484: E/AndroidRuntime(2160): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 03:42:02.484: E/AndroidRuntime(2160): at java.lang.reflect.Method.invoke(Method.java:525)
09-10 03:42:02.484: E/AndroidRuntime(2160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-10 03:42:02.484: E/AndroidRuntime(2160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 03:42:02.484: E/AndroidRuntime(2160): at dalvik.system.NativeStart.main(Native Method)