我试图搜索我的问题,但没有成功。
我从数据库(Parse.com)填充ArrayAdapter<String> parkingLotAdapter
。我将此适配器分配给微调器。 Spinner工作正常,显示项目列表。但是当我尝试通过getPosition(item)
获取适配器中的一个项目时,我得到NullPointerException。
List<String> list = new ArrayList<String>();
for (ParseObject temp : parkingList) {
list.add(temp.getString("parking_lot"));
}
parkingLotAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, list);
parkingLotAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
parkinglotSpinner.setAdapter(parkingLotAdapter);
int pos4 = parkingLotAdapter.getPosition("A123");
parkinglotSpinner.setSelection(pos4);
当我将getPosition(item)
与其他我从参考资料中填充并使用ArrayAdapter<CharSequence>
的微调器一起使用时,一切正常。你能帮我解决这个问题。
我的Logcat:
06-17 09:40:13.927: W/dalvikvm(26031): threadid=1: thread exiting with uncaught exception (group=0x41307300)
06-17 09:40:13.927: E/AndroidRuntime(26031): FATAL EXCEPTION: main
06-17 09:40:13.927: E/AndroidRuntime(26031): java.lang.NullPointerException
06-17 09:40:13.927: E/AndroidRuntime(26031): at me.aizon.app.fragments.RegisterFragment$LoadParkingSpinnerASYNC.onPostExecute(RegisterFragment.java:396)
06-17 09:40:13.927: E/AndroidRuntime(26031): at me.aizon.app.fragments.RegisterFragment$LoadParkingSpinnerASYNC.onPostExecute(RegisterFragment.java:1)
06-17 09:40:13.927: E/AndroidRuntime(26031): at android.os.AsyncTask.finish(AsyncTask.java:631)
06-17 09:40:13.927: E/AndroidRuntime(26031): at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-17 09:40:13.927: E/AndroidRuntime(26031): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-17 09:40:13.927: E/AndroidRuntime(26031): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 09:40:13.927: E/AndroidRuntime(26031): at android.os.Looper.loop(Looper.java:137)
06-17 09:40:13.927: E/AndroidRuntime(26031): at android.app.ActivityThread.main(ActivityThread.java:4746)
06-17 09:40:13.927: E/AndroidRuntime(26031): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 09:40:13.927: E/AndroidRuntime(26031): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 09:40:13.927: E/AndroidRuntime(26031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
06-17 09:40:13.927: E/AndroidRuntime(26031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
06-17 09:40:13.927: E/AndroidRuntime(26031): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
Try this way,hope this will help you to solve your problem.
You have already list object to try to get index from list instead of adapter.
List<String> list = new ArrayList<String>();
Replace this code:
int pos4 = parkingLotAdapter.getPosition("A123");
parkinglotSpinner.setSelection(pos4);
With this:
parkinglotSpinner.setSelection(list.indexOf("A123"))