我想在listview中添加catnames。但是它显示我的运行时错误。这是我的代码和logcat.i能够添加静态数组但是当我动态使用它时显示mw错误。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView attribute[];
ArrayAdapter adapter;
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL("http://mobileecommerce.site247365.com/admin/catdata.xml");
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
}catch(Exception e)
{
System.out.println("XML Pasing Excpetion = " + e);
}
sitesList = MyXMLHandler.sitesList;
Cat_id = new TextView[sitesList.getCat_id().size()];
Cat_Parentid = new TextView[sitesList.getCat_id().size()];
Cat_Name = new TextView[sitesList.getCat_id().size()];
Cat_Desc=new TextView[sitesList.getCat_id().size()];
String mystr,newstr;
for (int i = 0; i <sitesList.getCat_Name().size(); i++)
{
catNames=new String[sitesList.getCat_Name().size()];
Cat_Name[i] = new TextView(this);
Cat_Name[i].setText("Cat_Name = "+sitesList.getCat_Name().get(i));
catNames[i]=sitesList.getCat_Name().get(i);
Log.v("catnames",catNames[i]);
str[i]=sitesList.getCat_Desc().get(i);
temp[i]=str[i];
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,catNames));
getListView().setTextFilterEnabled(true);
}
static final String[] PENS = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"Rotring",
"Sheaffer",
"Waterman"
};
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen ::" + " " + pen, Toast.LENGTH_LONG).show();
}
我的LOGCAT -
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): FATAL EXCEPTION: main
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): java.lang.NullPointerException
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.ListView.makeAndAddView(ListView.java:1727)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.ListView.fillDown(ListView.java:652)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.ListView.fillFromTop(ListView.java:709)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.ListView.layoutChildren(ListView.java:1580)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.view.View.layout(View.java:7035)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.view.View.layout(View.java:7035)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.view.View.layout(View.java:7035)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.view.View.layout(View.java:7035)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.os.Looper.loop(Looper.java:123)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at java.lang.reflect.Method.invoke(Method.java:521)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-17 19:29:17.230: ERROR/AndroidRuntime(2517): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您有一个NullPointerException,因为未初始化适配器,它是一个ArrayAdapter。调用它的构造函数,正确初始化它,这个问题将成为历史。当您尝试调用此(不存在)对象的方法时,会发生运行时错误。如果为适配器赋值,则不应再使用此对象发生NullPointerException。您可能还有其他错误,但是我们的问题是当您调用未初始化对象的方法时抛出NullPointerException(这是一个没有初始化的空指针)。