Hannelore
Hannelore

Reputation: 763

Problem with AlertDialog (crash when trying to catch exception)

I want to catch exception when my XML doesn't parse. Because I have no influance on the XML being created, I wan't to catch exceptions when something goes wrong (like a wrong tags, weird signs and so on).

I've created following code, but it causes a crash. I've added LogCat under the code. I don't quite understand what LogCat is telling me... Anyone has a hint to help me? Thanks

private void getVacatures(){
    functie = getIntent().getIntExtra("Functie", 0);
    stat =getIntent().getIntExtra("Statuut", 0);
    regio = getIntent().getIntExtra("Straal", 0);
    opl = getIntent().getIntExtra("Opleiding", 0);
    final AlertDialog.Builder builder = new AlertDialog.Builder(JobList.this);

        try {
            Log.e("in try", "try");
            /* Create a URL we want to load some xml-data from. */

            URL url = new URL("C:/Users/hannelore.deblau/Desktop/TESTXML.xml");
            System.out.println("URL: "+ url);

            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();
            /* Create a new ContentHandler and apply it to the XML-Reader*/ 
            vacatureWebservice vs = new vacatureWebservice();
            xr.setContentHandler(vs);

            /* Parse the xml-data from our URL. */
            xr.parse(new InputSource(url.openStream()));
            /* Parsing has finished. */

            /* Our ExampleHandler now provides the parsed data to us. */
            arrVacatures = vs.getVacatures();
        } catch (Exception e) {
            builder.setTitle(R.string.Fouttitel);
            builder.setMessage(R.string.XMLfout);
            builder.setCancelable(false);
            builder.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            }); 
            builder.create().show();
        }
        //runOnUiThread(returnRes);
}

In onCreate() I do this:

Handler handler = new Handler(); // This code is in the main UI activity class

    handler.post(runnable= new Runnable(){
        public void run(){

            getVacatures();
            runOnUiThread(returnRes);
        }
    });
    Thread thread = new Thread(null, runnable, "MagentoBackground");
    thread.start();

LogCat with error: http://pastebin.com/X5uk9cex

Upvotes: 0

Views: 974

Answers (3)

Tanmay Mandal
Tanmay Mandal

Reputation: 40168

You are trying to update the Ui from a worker thread called MagentoBackground . You need to do your Ui work from within the main thread. Try to use Handler.

Here is some tutorial for Handler

Again in your are using an url like

  URL url = new URL("C:/Users/hannelore.deblau/Desktop/TESTXML.xml");

This URL will never invoke .What you need to do is to keep your xml in a web server then access it.

Edited Your onCreate

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.your_view);
          runnable= new Runnable() {
                    public void run() {
        // Your code 
    
    try{ /* Create a URL we want to load some xml-data from. */
    
                URL url = new URL("C:/Users/hannelore.deblau/Desktop/TESTXML.xml");
                System.out.println("URL: "+ url);
    
                /* Get a SAXParser from the SAXPArserFactory. */
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
    
                /* Get the XMLReader of the SAXParser we created. */
                XMLReader xr = sp.getXMLReader();
                /* Create a new ContentHandler and apply it to the XML-Reader*/ 
                vacatureWebservice vs = new vacatureWebservice();
                xr.setContentHandler(vs);
    
                /* Parse the xml-data from our URL. */
                xr.parse(new InputSource(url.openStream()));
                /* Parsing has finished. */
    
   

                 /* Our ExampleHandler now provides the parsed data to us. */
                    arrVacatures = vs.getVacatures();
          runOnUiThread(returnRes);
    
    
    }
        catch(Exception e)
        {
    //here error comes
    //here also you need to use this code
       runOnUiThread(nullRes);
        }
                        }
                };


    Thread thread = new Thread(null, runnable, "MagentoBackground");
    thread.start();

}

Now here is your exception code

private Runnable nullRes= new Runnable() {
        public void run() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(JobList.this);
 builder.setTitle(R.string.Fouttitel);
            builder.setMessage(R.string.XMLfout);
            builder.setCancelable(false);
            builder.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            }); 
            builder.create().show();

        }
    };

Upvotes: 1

Luke Vo
Luke Vo

Reputation: 20658

I guess you're using AndEngine or something like that, and this sub is called from a sub-Thread instead of main activity. That's why you get this error:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Try call that sub outside of any thread (maybe UIThread or UpdateThread). Or, if you HAVE TO call it within those thread, call a Handler by the Main Activity and post it:

Handler handler = new Handler(); // This code is in the main UI activity class

And when you want to call it:

handler.post(new Runnable() {
    getVacatures();
});

Upvotes: 0

fleetway76
fleetway76

Reputation: 1640

RuntimeException isnt a subclass of Exception so your catch clause wont trap it. You could change your catch clause to catch Throwable which is the common superclass. This would at least solve the problem of your code not handling it. There are probably other problems at play though regarding the cause of the exception.

The exception is being thrown because there is not handler/looper for the dialog to use. You need the dialog code to run on the UI thread.

Upvotes: 0

Related Questions