gpt4 book ai didi

java - 在异步线程中解析时出现 fatal error (AsyncTask.doInBackground())

转载 作者:行者123 更新时间:2023-12-02 07:30:53 25 4
gpt4 key购买 nike

我在尝试从 AsyncTask 类的 doInBackground 方法中的 URL 解析 XML 时遇到了问题。在它起作用之前但在我进行一些更改之后(现在解析链接),最有趣的是。我检查过 - 链接提供了正确的 XML。解析器在某个时刻解析响应链接,但会生成 fatal error 。非常希望得到您的帮助。

//analized DB & create var arrays with info about markers
@Override
protected Void doInBackground(Void ... param) {

setButtonsClickListener();

needShowLocation =
saxParserGetLocationRate.getIdAndRate(myLocationLatitudeDouble, myLocationLongitudeDouble);

createArrayMarkersPlace();

return null;
}

解析器类

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
import android.widget.Toast;

public class SAXParserGetLocationRate {

//Обработчик событий парсера
DefaultHandler handler = new DefaultHandler() {

//Метод для отслеживания старта разбора документа
@Override
public void startDocument() {
Log.d("SAXParser", "start document");
}

// Метод для отслеживание конца разбора документа
@Override
public void endDocument()
{
needShowLocation = new NeedShowLocation(needShowLoactionId, needShowLoactionRate);
status = false;
flag = false;
Log.d("SAXParser", "end document");
}

//Метод для отслеживание открываемых тегов и их атрибутов
@Override
public void startElement(String uri, String lname, String qname, Attributes attr) {
if(lname.equals("status"))status = true;
Log.d("uri/lname/qname/attr/flagStatus", uri+"/"+lname+"/"+qname+"/"+attr.getLength()+"/"+flag);
if(lname.equals("object") && flag){
Log.d("/", "/");
for(int i=0;i<attr.getLength();i++){
Log.d("*", "*");
if(i==0){
Log.d("-", "-");
Integer q = Integer.valueOf(attr.getValue(i));
Log.d("+", "+");
needShowLoactionId.add(q);
Log.d("needShowLoactionId.add", Integer.valueOf(attr.getValue(i))+"");
}

if(i==1){
atmosphearRating = Integer.valueOf(attr.getValue(i));
}

if(i==2){
peoplemeterRating = Integer.valueOf(attr.getValue(i));
needShowLoactionRate.add(Float.valueOf((atmosphearRating + peoplemeterRating)/2));
Log.d("needShowLoactionRate.add", ((atmosphearRating + peoplemeterRating)/2)+"");
}
}
}
}

// Метод для отслеживания конца документа
@Override
public void endElement(String uri, String lname, String qname) {
//
}

// Метод для изьятия символьных даных тегов
@Override
public void characters (char[] data, int start, int lenght) throws SAXException {
super.characters(data, start, lenght); // вызов метода с предыдущей реализацией
StringBuilder lineBuilder = new StringBuilder(); // создани экземпляра класа для формирования строки
lineBuilder.append(data, start, lenght); //формирование строки на основе масива символов, откуда изымаються элементы в количестве lenght начиная с элемента start
String line = lineBuilder.toString().trim(); //создание строки с усеканием пробелов в начале и конце
if(!(line.length()==0)) // условие проверяет не пустая ли строка
{
if(status && line.equals("ok")){
flag = true;
status = false;
Log.d("need show location", line);
}
if(status && line.equals("error")){
this.endDocument();
Log.d("need show location", line);
}
}
}
};

public NeedShowLocation getIdAndRate(double latitude, double longitude) {
Log.d("latitude/longitude", latitude+"/"+longitude);
SAXParserFactory factory = SAXParserFactory.newInstance(); // создание класа-фабрики для парсера
factory.setNamespaceAware(true); //установка подержки пронстрансва имён
try {
saxParser = factory.newSAXParser(); //создание класа парсера
} catch (ParserConfigurationException e1) {
System.out.println("ParserConfigurationException "+e1);
} catch (SAXException e1) {
System.out.println("SAXException "+e1);
}
Log.d("source_parse", "http://&&&.&&&.&&&/places_data/?lat="+latitude+"&lng="+longitude);
try {
saxParser.parse("http://&&&.&&&.&&&/places_data/?lat="+latitude+"&lng="+longitude, handler);
} catch (SAXException e) {
System.out.println("SAXException "+e);
} catch (IOException e) {
System.out.println("IOException "+e);
}
return needShowLocation;
}
private NeedShowLocation needShowLocation = null;
private SAXParser saxParser;
private ArrayList<Integer> needShowLoactionId;
private ArrayList<Float> needShowLoactionRate;
private int atmosphearRating;
private int peoplemeterRating;
private boolean status = false;
private boolean flag = false;
}

日志

"$ 10-17 15:51:57.999: D/Facebook-Util(13671): GET URL: https://graph.facebook.com/me?access_token=AAAFGvgaKHNgBAJdQwSkb5j1hXG8b61RitnEjIW3vpiMnemIIDYh1lA9LDz9BNx9NZAp5sCoDqIDfRygPL7eHtlhtY50TTJZCWDiybn1QZDZD&format=json 10-17 15:51:58.639: D/dalvikvm(13671): GC_CONCURRENT freed 228K, 49% free 2884K/5639K, external 611K/1112K, paused 8ms+10ms 10-17 15:51:58.989: D/Expir.onPause()(13671): START 10-17 15:51:58.989: D/Expir.onPause()(13671): FINISH 10-17 15:51:59.389: D/dalvikvm(13671): GC_CONCURRENT freed 306K, 48% free 3112K/5895K, external 611K/1112K, paused 9ms+7ms 10-17 15:51:59.479: D/onCreate()(13671): START 10-17 15:51:59.639: D/dalvikvm(13671): GC_CONCURRENT freed 537K, 50% free 3134K/6151K, external 611K/1112K, paused 4ms+10ms 10-17 15:51:59.829: D/dalvikvm(13671): GC_CONCURRENT freed 381K, 47% free 3265K/6151K, external 673K/1112K, paused 4ms+5ms 10-17 15:52:00.039: D/dalvikvm(13671): GC_CONCURRENT freed 519K, 48% free 3279K/6279K, external 673K/1112K, paused 4ms+6ms 10-17 15:52:00.199: D/dalvikvm(13671): GC_CONCURRENT freed 329K, 45% free 3462K/6279K, external 673K/1112K, paused 5ms+5ms 10-17 15:52:00.369: D/szipinf(13671): Initializing inflate state 10-17 15:52:00.469: D/onCreate()(13671): FINISH 10-17 15:52:00.469: D/onStart()(13671): START 10-17 15:52:00.469: D/onStart()(13671): FINISH 10-17 15:52:00.469: D/onResume()(13671): START 10-17 15:52:00.479: D/onResume()(13671): FINISH 10-17 15:52:00.489: D/latitude/longitude(13671): 57.702966/11.973206 10-17 15:52:00.539: D/source_parse(13671): http://&&&.&&&.&&&/places_data/?lat=57.702966&lng=11.973206 10-17 15:52:00.599: I/MapActivity(13671): Handling network change notification:CONNECTED 10-17 15:52:00.599: E/MapActivity(13671): Couldn't get connection factory client 10-17 15:52:00.689: D/dalvikvm(13671): GC_CONCURRENT freed 558K, 49% free 3325K/6407K, external 910K/1112K, paused 4ms+9ms 10-17 15:52:00.799: D/dalvikvm(13671): GC_EXTERNAL_ALLOC freed 181K, 51% free 3144K/6407K, external 910K/1112K, paused 114ms 10-17 15:52:00.869: D/dalvikvm(13671): GC_FOR_MALLOC freed 1K, 51% free 3143K/6407K, external 1166K/1678K, paused 55ms 10-17 15:52:00.869: I/dalvikvm-heap(13671): Grow heap (frag case) to 6.693MB for 69582-byte allocation 10-17 15:52:00.929: D/dalvikvm(13671): GC_FOR_MALLOC freed <1K, 51% free 3211K/6535K, external 1166K/1678K, paused 53ms 10-17 15:52:01.029: D/dalvikvm(13671): GC_FOR_MALLOC freed 170K, 51% free 3258K/6535K, external 1166K/1678K, paused 56ms 10-17 15:52:01.139: D/dalvikvm(13671): GC_FOR_MALLOC freed 48K, 50% free 3292K/6535K, external 1166K/1678K, paused 58ms 10-17 15:52:01.139: I/dalvikvm-heap(13671): Grow heap (frag case) to 6.838MB for 69582-byte allocation 10-17 15:52:01.199: D/dalvikvm(13671): GC_FOR_MALLOC freed 63K, 51% free 3297K/6663K, external 1166K/1678K, paused 54ms 10-17 15:52:01.459: D/Expir.onStop()(13671): START 10-17 15:52:01.459: D/Expir.onStop()(13671): FINISH 10-17 15:52:01.659: D/SAXParser(13671): start document 10-17 15:52:01.659: D/uri/lname/qname/attr/flagStatus(13671): /content/content/0/false 10-17 15:52:01.659: D/uri/lname/qname/attr/flagStatus(13671): /status/status/0/false 10-17 15:52:01.659: D/need show location(13671): ok 10-17 15:52:01.659: D/uri/lname/qname/attr/flagStatus(13671): /object/object/5/true 10-17 15:52:01.659: D//(13671): / 10-17 15:52:01.659: D/*(13671): * 10-17 15:52:01.659: D/-(13671): - 10-17 15:52:01.659: D/+(13671): + 10-17 15:52:01.669: W/dalvikvm(13671): threadid=9: thread exiting with uncaught exception (group=0x40018560) 10-17 15:52:01.679: E/AndroidRuntime(13671): FATAL EXCEPTION: AsyncTask #1 10-17 15:52:01.679: E/AndroidRuntime(13671): java.lang.RuntimeException: An error occured while executing doInBackground() 10-17 15:52:01.679: E/AndroidRuntime(13671):     at android.os.AsyncTask$3.done(AsyncTask.java:200) 10-17 15:52:01.679: E/AndroidRuntime(13671):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 10-17 15:52:01.679: E/AndroidRuntime(13671):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 10-17 15:52:01.679: E/AndroidRuntime(13671):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 10-17 15:52:01.679: E/AndroidRuntime(13671):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-17 15:52:01.679: E/AndroidRuntime(13671):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 10-17 15:52:01.679: E/AndroidRuntime(13671):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-17 15:52:01.679: E/AndroidRuntime(13671):     at java.lang.Thread.run(Thread.java:1019) 10-17 15:52:01.679: E/AndroidRuntime(13671): Caused by: java.lang.NullPointerException 10-17 15:52:01.679: E/AndroidRuntime(13671):   at expir.java.file.SAXParserGetLocationRate$1.startElement(SAXParserGetLocationRate.java:54) 10-17 15:52:01.679: E/AndroidRuntime(13671):   at org.apache.harmony.xml.ExpatParser.startElement(ExpatParser.java:145) 10-17 15:52:01.679: E/AndroidRuntime(13671):   at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method) 10-17 15:52:01.679: E/AndroidRuntime(13671):   at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518) 10-17 15:52:01.679: E/AndroidRuntime(13671):  at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479) 10-17 15:52:01.679: E/AndroidRuntime(13671):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318) 10-17 15:52:01.679: E/AndroidRuntime(13671):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:291) 10-17 15:52:01.679: E/AndroidRuntime(13671):  at javax.xml.parsers.SAXParser.parse(SAXParser.java:390) 10-17 15:52:01.679: E/AndroidRuntime(13671):   at javax.xml.parsers.SAXParser.parse(SAXParser.java:266) 10-17 15:52:01.679: E/AndroidRuntime(13671):   at expir.java.file.SAXParserGetLocationRate.getIdAndRate(SAXParserGetLocationRate.java:114) 10-17 15:52:01.679: E/AndroidRuntime(13671):    at expir.java.file.MyMapActivity$AsyncLoader.doInBackground(MyMapActivity.java:572) 10-17 15:52:01.679: E/AndroidRuntime(13671):    at expir.java.file.MyMapActivity$AsyncLoader.doInBackground(MyMapActivity.java:1) 10-17 15:52:01.679: E/AndroidRuntime(13671):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 10-17 15:52:01.679: E/AndroidRuntime(13671):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 10-17 15:52:01.679: E/AndroidRuntime(13671):  ... 4 more 10-17 15:52:01.689: D/onPause()(13671): START 10-17 15:52:01.719: D/onPause()(13671): FINISH 10-17 15:52:01.759: D/Expir.onRestart()(13671): START 10-17 15:52:01.759: D/Expir.onRestart()(13671): FINISH 10-17 15:52:01.759: D/Expir.onStart()(13671): START 10-17 15:52:01.759: D/Expir.onStart()(13671): FINISH 10-17 15:52:01.759: D/Expir.onResume()(13671): START 10-17 15:52:01.759: D/Expir.onResume()(13671): FINISH 10-17 15:52:02.049: D/onStop()(13671): START 10-17 15:52:02.049: D/onStop()(13671): FINISH 10-17 15:52:02.049: D/onDestroy()(13671): START 10-17 15:52:02.049: D/onDestroy()(13671): FINISH 10-17 15:52:02.079: E/WindowManager(13671): Activity expir.java.file.MyMapActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@406318d8 that was originally added here 10-17 15:52:02.079: E/WindowManager(13671): android.view.WindowLeaked: Activity expir.java.file.MyMapActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@406318d8 that was originally added here 10-17 15:52:02.079: E/WindowManager(13671):     at android.view.ViewRoot.<init>(ViewRoot.java:259) 10-17 15:52:02.079: E/WindowManager(13671):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 10-17 15:52:02.079: E/WindowManager(13671):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 10-17 15:52:02.079: E/WindowManager(13671):    at android.view.Window$LocalWindowManager.addView(Window.java:465) 10-17 15:52:02.079: E/WindowManager(13671):  at android.app.Dialog.show(Dialog.java:241) 10-17 15:52:02.079: E/WindowManager(13671):     at expir.java.file.MyMapActivity$AsyncLoader.onPreExecute(MyMapActivity.java:562) 10-17 15:52:02.079: E/WindowManager(13671):   at android.os.AsyncTask.execute(AsyncTask.java:391) 10-17 15:52:02.079: E/WindowManager(13671):     at expir.java.file.MyMapActivity.onCreate(MyMapActivity.java:75) 10-17 15:52:02.079: E/WindowManager(13671):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10-17 15:52:02.079: E/WindowManager(13671):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722) 10-17 15:52:02.079: E/WindowManager(13671):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784) 10-17 15:52:02.079: E/WindowManager(13671):    at android.app.ActivityThread.access$1500(ActivityThread.java:123) 10-17 15:52:02.079: E/WindowManager(13671):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 10-17 15:52:02.079: E/WindowManager(13671):  at android.os.Handler.dispatchMessage(Handler.java:99) 10-17 15:52:02.079: E/WindowManager(13671):  at android.os.Looper.loop(Looper.java:130) 10-17 15:52:02.079: E/WindowManager(13671):  at android.app.ActivityThread.main(ActivityThread.java:3835) 10-17 15:52:02.079: E/WindowManager(13671):    at java.lang.reflect.Method.invokeNative(Native Method) 10-17 15:52:02.079: E/WindowManager(13671):     at java.lang.reflect.Method.invoke(Method.java:507) 10-17 15:52:02.079: E/WindowManager(13671):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 10-17 15:52:02.079: E/WindowManager(13671):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 10-17 15:52:02.079: E/WindowManager(13671):     at dalvik.system.NativeStart.main(Native Method) 10-17 15:57:01.779: I/Process(13671): Sending signal. PID: 13671 SIG: 9 "

最佳答案

needShowLoactionIdneedShowLoactionRatenull 时,您将向它们添加对象。在startElement中:

needShowLoactionId.add(q);
...
needShowLoactionRate.add(Float.valueOf((atmosphearRating + peoplemeterRating)/2));

堆栈跟踪显示异常原因是 NullPointerException,发生在 SAXParserGetLocationRate$1.startElement(第 54 行)中。 (“$1”表示 startElement 方法出现在 SAXParseGetLocationRate 内的第一个匿名内部类中 - 在本例中为 DefaultHandler。)

要解决此问题,请更改以下行:

private ArrayList<Integer> needShowLoactionId;
private ArrayList<Float> needShowLoactionRate;

...像这样:

private ArrayList<Integer> needShowLoactionId = new ArrayList<Integer>();
private ArrayList<Float> needShowLoactionRate = new ArrayList<Float>();

关于java - 在异步线程中解析时出现 fatal error (AsyncTask.doInBackground()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com