NullPointerException error when polling down the preset url list spinner

There was a bug in RTSPPlayer, that is when you poll down the preset RTSP stream source  list spinner, the APP will crash with an NullPointerException. Today, I finally have it fixed.

Error message detail:

[code]

04-29 14:47:07.610: W/dalvikvm(8068): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
04-29 14:47:07.690: E/AndroidRuntime(8068): FATAL EXCEPTION: main
04-29 14:47:07.690: E/AndroidRuntime(8068): java.lang.NullPointerException
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:336)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:332)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.AbsListView.obtainView(AbsListView.java:1533)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.ListView.makeAndAddView(ListView.java:1793)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.ListView.fillDown(ListView.java:718)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.ListView.fillGap(ListView.java:689)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4142)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:2523)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.widget.ListView.onTouchEvent(ListView.java:3696)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.View.dispatchTouchEvent(View.java:3938)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1737)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1151)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.app.Dialog.dispatchTouchEvent(Dialog.java:642)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1721)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2217)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1888)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.os.Looper.loop(Looper.java:123)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at java.lang.reflect.Method.invoke(Method.java:507)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-29 14:47:07.690: E/AndroidRuntime(8068):     at dalvik.system.NativeStart.main(Native Method)

[/code]

Here is my solution for this:

[java]
/*定义创建一个List<T>*/
private String[] cities;
private List<String> cityList;
/*初始化字符串数组*/
cities = new String[] { "Android", "BlackBerry", "J2ME", "Symbian",
"Broncho", "LinuxMobile", "Palm", "WindwosMobile" };
/*初始化List实例ArrayList的对象*/
cityList = new ArrayList<String>();
/*遍历,把字符串数组添加到ArrayList中*/
for (int i = 0; i < cities.length; i++) {
cityList.add(cities[i]);
}
// 定义创建一个ArrayAdapter<String>
/*定义*/
private ArrayAdapter<String> arrayAdapter;
/* 这里应该使用List<> ,如果使用String[]则会出错*/
/*初始化下拉菜单的内容适配器*/
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, cityList);
//设置ArrayAdapter<String>在下拉菜单中的显示布局
/*设置下拉菜单显示内容的风格*/
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//给下拉菜单添加内容Adapter 适配器
/*给下拉菜单对象添加内容适配器*/
city_Spinner.setAdapter(arrayAdapter);

[/java]

Author: Jacky Wei

I am a programmer, welcome to my blog: http://rg4.net.

One thought on “NullPointerException error when polling down the preset url list spinner”

  1. I have been examinating out some of your articles and it’s pretty clever stuff. I will definitely bookmark your website.

Leave a Reply

Your email address will not be published. Required fields are marked *