以下代码片段的目标是用于解析 xml。在那个 AsynchTask 用于互联网连接,但是我在 for 循环之前的行上得到了 NullPointerException。这是一个片段和堆栈跟踪。
public class MainActivity extends Activity {
static final String KEY_ITEM = "photo"; // parent node
String xml;
Document doc;
NodeList nl;
DefaultHttpClient httpClient;
HttpPost httpPost;
HttpResponse httpRes;
HttpEntity httpEnt;
String xmlurl = "http://url.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> photoLink = new ArrayList<String>();
new FileFromURL().execute(xmlurl);
nl = doc.getElementsByTagName("photo"); /////// line 62
for(int i=0; i < nl.getLength(); i++){
Node node = nl.item(i);
Element fstElmnt = (Element) node;
photoLink.add(fstElmnt.getAttribute("link"));
}
for(int i=0;i<photoLink.size();i++){
Log.d("Photo link --- " + i,photoLink.get(i));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class FileFromURL extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... xmlur) {
// TODO Auto-generated method stub
try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(xmlurl);
httpRes = httpClient.execute(httpPost);
httpEnt = httpRes.getEntity();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
try{
xml = EntityUtils.toString(httpEnt);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
}catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPostExecute(result);
}
}
}
堆栈跟踪 :
12-05 11:01:34.128: E/AndroidRuntime(11990): FATAL EXCEPTION: main
12-05 11:01:34.128: E/AndroidRuntime(11990): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gallery.parsing.madhuri/com.example.gallery.parsing.madhuri.MainActivity}: java.lang.NullPointerException
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.os.Looper.loop(Looper.java:137)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-05 11:01:34.128: E/AndroidRuntime(11990): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 11:01:34.128: E/AndroidRuntime(11990): at java.lang.reflect.Method.invoke(Method.java:511)
12-05 11:01:34.128: E/AndroidRuntime(11990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
12-05 11:01:34.128: E/AndroidRuntime(11990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-05 11:01:34.128: E/AndroidRuntime(11990): at dalvik.system.NativeStart.main(Native Method)
12-05 11:01:34.128: E/AndroidRuntime(11990): Caused by: java.lang.NullPointerException
12-05 11:01:34.128: E/AndroidRuntime(11990): at com.example.gallery.parsing.madhuri.MainActivity.onCreate(MainActivity.java:62)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.Activity.performCreate(Activity.java:4465)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-05 11:01:34.128: E/AndroidRuntime(11990): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-05 11:01:34.128: E/AndroidRuntime(11990): ... 11 more
因为您执行了 asyncTask,但随后尝试从 doc 创建 nodeList,这可能尚未初始化。你也应该把nodeList放到你的asyncTask中。
尝试这样做:
public class MainActivity extends Activity {
static final String KEY_ITEM = "photo"; // parent node
String xml;
Document doc;
NodeList nl;
DefaultHttpClient httpClient;
HttpPost httpPost;
HttpResponse httpRes;
HttpEntity httpEnt;
String xmlurl = "http://url.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> photoLink = new ArrayList<String>();
new FileFromURL().execute(xmlurl);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class FileFromURL extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... xmlur) {
// TODO Auto-generated method stub
try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(xmlurl);
httpRes = httpClient.execute(httpPost);
httpEnt = httpRes.getEntity();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try{
xml = EntityUtils.toString(httpEnt);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
}catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(doc != null) {
nl = doc.getElementsByTagName("photo"); /////// line 62
for(int i=0; i < nl.getLength(); i++){
Node node = nl.item(i);
Element fstElmnt = (Element) node;
photoLink.add(fstElmnt.getAttribute("link"));
}
for(int i=0;i<photoLink.size();i++){
Log.d("Photo link --- " + i,photoLink.get(i));
}
}
}
}
}
AsyncTask 类允许执行后台操作。
请参阅异步任务
public class MainActivity extends Activity {
static final String KEY_ITEM = "photo"; // parent node
String xml;
Document doc;
NodeList nl;
DefaultHttpClient httpClient;
HttpPost httpPost;
HttpResponse httpRes;
HttpEntity httpEnt;
String xmlurl = "http://url.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new FileFromURL().execute(xmlurl);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class FileFromURL extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... xmlur) {
// TODO Auto-generated method stub
try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(xmlurl);
httpRes = httpClient.execute(httpPost);
httpEnt = httpRes.getEntity();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
try{
xml = EntityUtils.toString(httpEnt);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
ArrayList<String> photoLink = new ArrayList<String>();
nl = doc.getElementsByTagName("photo"); /////// line 62
for(int i=0; i < nl.getLength(); i++){
Node node = nl.item(i);
Element fstElmnt = (Element) node;
photoLink.add(fstElmnt.getAttribute("link"));
}
for(int i=0;i<photoLink.size();i++){
Log.d("Photo link --- " + i,photoLink.get(i));
}
}catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPostExecute(result);
}
}
}