Value in spinner after XML parsing
I have a problem in showing value in spinner after XML parsing. I had done XML parsing and now I want to display the parsed result in spinner, but my application forces close when run. Code snippet is given below.
public class parsedspinner extends Activity {
String name = null;
private String array_spinner[];
/** Create Object For SiteList Class */
SitesList sitesList = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://www.arteonline.mobi/iphone/output.php?estado=1");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name=sitesList.getName().get(i);
array_spinner=new String[sitesList.getName().size()];
array_spinner[i]=name;
Spinner s = (Spinner) findViewById(R.id.Spinner01);
**//till here no problem** when using array adapter then forse close..
**ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter)**;
**when i comented array adapterand set adapter above then values also show in log cat then defnetly problem in above 2 lines what is not gettig????pls help me...**
Log.i("array_spinner"+i,array_spinner[i]);
}
}
error
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): FATAL EXCEPTION: main
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): java.lang.NullPointerException
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.View.measure(View.java:8313)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.View.measure(View.java:8313)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.FrameLayout.onMeasure(FrameLayout.开发者_运维百科java:250)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.View.measure(View.java:8313)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.View.measure(View.java:8313)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.View.measure(View.java:8313)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.os.Looper.loop(Looper.java:123)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at java.lang.reflect.Method.invoke(Method.java:507)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-16 15:51:33.506: ERROR/AndroidRuntime(1229): at dalvik.system.NativeStart.main(Native Method)
05-16 15:51:33.525: WARN/ActivityManager(60): Force finishing
You should remove the
array_spinner
initialization, and theArrayAdapter
declaration and attaching code from yourfor
cycle,
so it won't apply for every item in your list:
array_spinner = new String[sitesList.getName().size()];
for (int i = 0; i < sitesList.getName().size(); i++)
{
name = sitesList.getName().get(i);
array_spinner[i] = name;
Log.i("array_spinner" + i, array_spinner[i]);
}
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
The NullPointerException
was thrown because of the array_spinner
got initialized as many times as much items you've got, and so it only contained one data, on the other positions it had null
.
Update
If you don't really need that array_spinner
String array, you could avoid it, initializing your ArrayAdapter
as:
sitesList = MyXMLHandler.sitesList;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.simple_spinner_item, sitesList.getName());
s.setAdapter(adapter);
This way you won't need that whole for
cylcle at all!
精彩评论