이 코드는 로컬로 저장된 xml 파일을 구문 분석 한 다음 구문 분석 된 데이터에서 동적 UI를 작성합니다. . 그것은 어제까지 잘 작동하고 있었고 갑자기 응답하지 않습니다.AsyncTask를 사용하여 메인 스레드의로드를 완화 한 후에도 응용 프로그램이 응답하지 않는다고 말합니다.
public class XML_PARSER extends Activity {
String TAG= "XML_PARSER";
List optionList = new ArrayList(); ;
Document dom;
Document doc;
Menu mymenu=null;
String msg=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parser);
new ParseXML().execute();
}
private class ParseXML extends AsyncTask<Integer, Integer, Document>{
@Override
protected Document doInBackground(Integer... params) {
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
Document dom1=null;
try {
//Uri uri = Uri.parse("android.resource://com.example.xml_parser/raw/options");
InputStream is=getResources().openRawResource(R.raw.options);
DocumentBuilder db = dbf.newDocumentBuilder();
dom1=db.parse(is);
Log.i(TAG,"parsing done");
}
catch(ParserConfigurationException pce){
pce.printStackTrace();
}
catch(SAXException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
return dom1;
}
@Override
public void onPostExecute(Document d) {
ParseDocument(d);
//onCreateOptionsMenu(mymenu);
text();
}
}
@SuppressWarnings("unchecked")
public void ParseDocument(Document dom){
Element docEle = dom.getDocumentElement();
Node node;
NodeList n1= docEle.getElementsByTagName("Option");
if(n1!=null && n1.getLength()>0){
for(int i=0;i<n1.getLength();i++){
node=n1.item(i);
Element e1=(Element)n1.item(i);
이 어떻게 든 그 위에 기록 된 경우 인쇄하기 경우 아래에 기록 된 경우 로그 인쇄하기되지 않는이 위해 getOption()에 갇히지된다. onPostExecuted
에서 호출 된 방법으로
Option e = getOption(e1,node);
Log.i(TAG,"Parse Document reached");
optionList.add(e);
}
}
}
private Option getOption(Element el,Node parent){
String name= getTextValue(el,"Name");
String type = el.getAttribute("type");
Option op = new Option(name,type);
fillSubOptions(op, el, parent);
return op;
}
private String getTextValue(Element ele, String tagName){
String textVal=null;
NodeList n1 = ele.getElementsByTagName(tagName);
if(n1!=null && n1.getLength()>0){
Element el= (Element)n1.item(0);
textVal =el.getFirstChild().getNodeValue();
}
return textVal;
}
private void fillSubOptions(Option op, Element el, Node parent){
Element ele;
Node node;
String name = null;
NodeList n1 = el.getElementsByTagName("Option");
int count =0;
if(n1!=null && n1.getLength()>0){
for(int i=0;i<n1.getLength();i++){
ele = (Element)n1.item(i);
node= n1.item(i);
if((node.getParentNode()).equals(parent)){
name= getTextValue(ele, "Name");
count= op.printSubOptions(count);
op.setSubOptions(name);
}
}
}
}
@SuppressWarnings("unchecked")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mymenu=menu;
Iterator<Option> it= optionList.iterator();
int count=0;
while(it.hasNext()){
Option e= (Option)it.next();
if(e.getType().equals("menu")){
count++;
menu.add(0,count,0,e.getName());
}
}
getMenuInflater().inflate(R.menu.parser, menu);
return true;
}
public class Option {
String name;
String type;
String parent;
int count;
ArrayList<String> subOptions;
Option(String name,String type)
{
setName(name);
setType(type);
subOptions = new ArrayList<String>();
parent = null;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setSubOptions(String subOption) {
(this.subOptions).add(subOption);
}
public String getName() {
return name;
}
public int printSubOptions(int count) {
Iterator<String> it = (this.subOptions).iterator();
if(it.hasNext()){
while(it.hasNext()){
count++;
}
}
return count;
}
public String getType() {
return type;
}
}
구문 분석 된 XML에서 UI를 만드는 것이 목적이라면 왜 AsyncTask를 사용합니까? – Niko
그리고 질문 자체를 위해 getOption, fullSubOptions 등에서 주석을 추가하거나 중단 점을 사용하여 디버깅하는 등의 방법으로 주석을 매우 쉽게 찾을 수 있습니다. – Niko
ANR 로그 파일을 확인 했습니까? –