Greg Greg - 1 month ago 7
Android Question

HTC One mini crashes XML parser

Device: HTC One mini

OS version: 4.3

Problem: AsyncTask XML reader crashes without triggering UncaughtExceptionHandler

.

I made a simple AsyncTask XML reader / parser that runs on most devices (OS 2.2 and older), but not on my HTC One mini (OS 4.3). When I try to run the AsyncTask, it always crashes at some point in code of doInBackground. Sometimes its on url.openConnection(), sometimes at httpurlconection.getInputStream(), sometimes at if(stream!=null)stream.close();, sometimes at some other random point in code. On very rare occasions the AsyncTask can finish itself, but crashes on the next request anyway.

The main loop is just waiting for the AsyncTask process to stop, by running a volatile boolean in while(XMLreaderAsyncTask.parsingnotComplete);

The kicker is that I use a DefaultUncaughtExceptionHandler to write a TXT file report on all crashes, but it doesn’t work/trigger for this type of crash. It works fine if I trigger it by going out of bounds of an array, but not for what is causing the crash of the AsyncTask XML reader.

public static Thread.UncaughtExceptionHandler androidDefaultUEH;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...

androidDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
Write a TXT report file
androidDefaultUEH.uncaughtException(paramThread, paramThrowable);
}
...
}


I cant get eclipse to detect the phone as plugged in, so I cant get the LogCat report. At best, I put up checkpoint flags by writing into SharedPreferences

EDIT 1:
Calling XML AsyncTask

HandleXMLbackground XMLURL2=new HandleXMLbackground(contextvar,"http:/www.demowebsite.com/pageurloutput.xml");
XMLURL2.execute();
while(XMLURL2.parsingnotComplete);
String x=XMLURL2.xml_unparsed;


Code of AsyncTask

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Xml;

public class HandleXMLbackground extends AsyncTask<String, Void, Void>
{
public volatile boolean parsingnotComplete = true;
private String urlString = "";
static Context contxtxml;

public String xml_unparsed="";

public HandleXMLbackground(Context c,String url)
{
contxtxml=c;
urlString=url;
}


public void parse(InputStream in)
{
LogLastAction5("parse start "+urlString);

try
{
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();

//Parsing happens here

}
catch (XmlPullParserException e)
{
LogLastAction5("xml F1 "+urlString+" "+e.getMessage()+" "+e.toString());
}
catch (IOException e)
{
LogLastAction5("xml F2 "+urlString+" "+e.getMessage()+" "+e.toString());
}
}

@Override
protected Void doInBackground(String... urls)
{
parsingnotComplete = true;
LogLastAction5("start "+urlString);

try
{
URL url = new URL(urlString);
HttpURLConnection universalconn = (HttpURLConnection) url.openConnection();
universalconn.setConnectTimeout(7000);//ms
universalconn.setReadTimeout(8000);//ms
universalconn.setRequestMethod("GET");
universalconn.setDoInput(true);
universalconn.connect();
InputStream stream2 = universalconn.getInputStream();
parse(stream2);
if(stream2!=null)stream2.close();
universalconn.disconnect();
}
catch (IOException e)
{
LogLastAction5("fail 1 "+urlString+" "+e.getMessage()+" "+e.toString());
}

parsingnotComplete = false;
return null;
}

protected static void LogLastAction5(String tx)
{
SharedPreferences settings1 = contxtxml.getSharedPreferences("com.myapp.stuff", Context.MODE_PRIVATE);
SharedPreferences.Editor seditor = settings1.edit();
seditor.putString("LastAct5","xml "+tx);
seditor.apply();
}
}


EDIT 2:
I tried making the class into private class of Activity and added the onPostExecute. Still crashed on HTC One mini 9 times out of 9 on these commands:

1 universalconn.setRequestMethod("GET");
2 universalconn.connect();
3 stream2 = universalconn.getInputStream();
4 universalconn.setConnectTimeout(7000);//ms
5 stream2 = universalconn.getInputStream();
6 stream2 = universalconn.getInputStream();
7 universalconn.setConnectTimeout(7000);//ms
8 universalconn.setDoInput(true);
9 universalconn.setDoInput(true);


EDIT 4:
Found how to make phone go into developer mode, so now Eclipse shows logcat reports.
The overall report is a too long to post, but it ends in 2 red lines.
Will this help?

03-03 16:14:06.723: E/dalvikvm(20345): threadid=13: stuck on threadid=1, giving up
03-03 16:14:06.723: A/libc(20345): Fatal signal 16 (SIGSTKFLT) at 0x00004f79 (code=-6), thread 20345 (evelads.navimon)


I also had "threadid=13" be "threadid=11"

EDIT 5: Tested the code on different devices
Models the code works fine:

Samsung Galaxy y gt-s5360 (Android OS 2.3.6)
Samsung GT-N8000 (Android OS 4.1.2)
Samsung GT-I5500 (Android OS 2.3.7)
HTC One V (Android OS 4.0.3)


Models the code crashes 99.999% of the time:

HTC One Mini (Android OS 4.2)
Cube Talk 7X U51GT-C4 (Android OS 4.2.2)

Answer Source

After looking more into the issue I found the solution here. Evidently some phone models don't like the continuous uninterrupted loops and consider them an extraordinary crash scenario.

So my problem was in this:

while(XMLURL2.parsingnotComplete);

I changed it to this, and now don't get the crashing.

while(XMLURL2.parsingnotComplete)
{
    try
    {
        Thread.sleep(100);
    }
    catch (InterruptedException e){}
}