我想在一個spinner中添加另一個類的list中的items。使用的下面的代碼但是給出null pointer exception。
ArrayAdapter<Address> adapter = new ArrayAdapter<Address>(
this,
android.R.layout.simple_spinner_item,
HomePage.NearPlaces
);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item
);
Place.setAdapter(adapter);
LogCat 輸出:
INFO/ActivityManager(96): Starting: Intent { cmp=com.emx.OnDaMove/.SpotMe } from pid 20881
WARN/ActivityManager(96): Activity pause timeout for HistoryRecord{40705eb0 com.emx.OnDaMove/.HomePage}
DEBUG/AndroidRuntime(20881): Shutting down VM
WARN/dalvikvm(20881): threadid=1: thread exiting with uncaught exception (group=0x40015560)
ERROR/AndroidRuntime(20881): FATAL EXCEPTION: main
ERROR/AndroidRuntime(20881): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emx.OnDaMove/com.emx.OnDaMove.SpotMe}: java.lang.NullPointerException
ERROR/AndroidRuntime(20881): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
ERROR/AndroidRuntime(20881): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
ERROR/AndroidRuntime(20881): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
ERROR/AndroidRuntime(20881): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
ERROR/AndroidRuntime(20881): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(20881): at android.os.Looper.loop(Looper.java:130)
ERROR/AndroidRuntime(20881): at android.app.ActivityThread.main(ActivityThread.java:3683)
ERROR/AndroidRuntime(20881): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(20881): at java.lang.reflect.Method.invoke(Method.java:507)
ERROR/AndroidRuntime(20881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ERROR/AndroidRuntime(20881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
ERROR/AndroidRuntime(20881): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(20881): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(20881): at com.emx.OnDaMove.SpotMe.onCreate(SpotMe.java:42)
ERROR/AndroidRuntime(20881): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(20881): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
ERROR/AndroidRuntime(20881): ... 11 more
//設置下拉列表的風格
adapter = ArrayAdapter.createFromResource(this,R.array.business, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
/**
* 設置第一組下拉列表內容
*/
hangye1 = (Spinner)this.findViewById(R.id.spinner_hangye1);
hangye1.setPrompt("所屬行業");
//將adapter添加到spinner中
hangye1.setAdapter(adapter);
//添加事件Spinner事件監聽
hangye1.setOnItemSelectedListener(selectHangye1);
//設置默認值
hangye1.setVisibility(View.VISIBLE);
R.array.business是在資源文件裡定義的下拉列表裡的選項