问题描述
我在显示XML解析后的微调值的一个问题。我做了XML解析,现在我想以显示微调解析结果,但收盘时运行我的应用程序的力量。 code片段如下。
公共类parsedspinner延伸活动{
字符串名称= NULL;
私人字符串array_spinner []; / **创建对象对于站点列表类* /
SitesList sitesList = NULL; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 尝试{
SPF SAXParserFactory的= SAXParserFactory.newInstance();
的SAXParser SP = spf.newSAXParser();
XMLReader的XR = sp.getXMLReader(); / **发送URL解析XML标签* /
网址sourceUrl =新的URL(
http://www.arteonline.mobi/iphone/output.php?estado=1); / **创建处理程序来处理XML标签(扩展的DefaultHandler)* /
MyXMLHandler myXMLHandler =新MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(新的InputSource(sourceUrl.openStream())); }赶上(例外五){
的System.out.println(XML帕兴错误时抛出=+ e)条;
}
/ **从MyXMLHandler SitlesList对象*获取结果/
sitesList = MyXMLHandler.sitesList;
/ **指定由ArrayList的大小TextView的阵列lenght * /
/ **设置在TextView的结果文本,并把它添加到布局* /
的for(int i = 0; I< sitesList.getName()大小();我++){ 名称= sitesList.getName()得到(I)。
array_spinner =新的String [sitesList.getName()大小()];
array_spinner [I] =名称;
微调S =(微调)findViewById(R.id.Spinner01);
** //到这里没有问题,使用数组适配器时**然后ForSE召开前夕接近.. ** ArrayAdapter适配器=新ArrayAdapter(这一点,
android.R.layout.simple_spinner_item,array_spinner);
s.setAdapter(适配器)**;
**当我comented阵列adapterand上方,然后设置适配器值也显示在日志猫2行以上则defnetly问题什么是不gettig ????请帮我... ** Log.i(array_spinner+ I,array_spinner [I]);
}
}
错误
15 05-16:51:33.506:ERROR / AndroidRuntime(1229):致命异常:主要
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):显示java.lang.NullPointerException
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.View.measure(View.java:8313)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.View.measure(View.java:8313)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.View.measure(View.java:8313)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.View.measure(View.java:8313)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.View.measure(View.java:8313)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.ViewRoot.performTraversals(ViewRoot.java:839)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.os.Handler.dispatchMessage(Handler.java:99)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.os.Looper.loop(Looper.java:123)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在android.app.ActivityThread.main(ActivityThread.java:3683)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在java.lang.reflect.Method.invokeNative(本机方法)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在java.lang.reflect.Method.invoke(Method.java:507)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-16 15:51:33.506:ERROR / AndroidRuntime(1229):在dalvik.system.NativeStart.main(本机方法)
05-16 15:51:33.525:WARN / ActivityManager(60):强制整理
您应该删除
-
array_spinner
初始化,
在 -
ArrayAdapter
申报
从附加code为
周期
所以它不会申请中的每一项
您的列表:
array_spinner =新的String [sitesList.getName()大小()];
的for(int i = 0; I< sitesList.getName()大小();我++)
{
名称= sitesList.getName()得到(I)。
array_spinner [I] =名称;
Log.i(array_spinner+ I,array_spinner [I]);
}
微调S =(微调)findViewById(R.id.Spinner01);
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
android.R.layout.simple_spinner_item,array_spinner);
s.setAdapter(适配器);
的 NullPointerException异常
是因为你已经得到了 array_spinner
得到了多次初始化尽可能多的物品抛出,所以它仅包含一个数据,它有空
的其他职位。
更新
如果你并不真的需要一个 array_spinner
String数组,你可以避开它,你的初始化 ArrayAdapter
作为
sitesList = MyXMLHandler.sitesList;
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
R.layout.simple_spinner_item,sitesList.getName());
s.setAdapter(适配器);
这样,您就不再需要全为
cylcle了!
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, andtheArrayAdapter
declaration andattaching 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!
这篇关于值微调XML解析后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!