-1

My application crashes when I minimize, turn off the 3G and return to the application. This application captures XML information from internet(SAX Parser) then by pressing the home button, turn off the 3G and returning to the app looks in black.

Thanks for your help

My Main code

    SitesList5101 sitesList5101 = null;


static final int DIALOG_ERROR_CONNECTION = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xmlultimoterceroo);

    TextView nemotecnico[];
    TextView precio[];
    TextView variacion[];
    TextView ttransado[];
    TextView pcierre[];
    TextView utrans[];

    TableRow tr[] = new TableRow[108];
    TableLayout layout = (TableLayout)findViewById(R.id.layout);


            try{

                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();

                URL sourceUrl = new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=5101");
                MyXMLHandler5101 myXMLHandler = new MyXMLHandler5101();
                xr.setContentHandler(myXMLHandler);
                xr.parse(new InputSource(sourceUrl.openStream()));

            } catch (Exception e){
                System.out.println("XML PArsing Exc =" +e);
            }




    sitesList5101 = MyXMLHandler5101.sitesList5101;

    nemotecnico = new TextView[sitesList5101.getNemotecnico().size()];
    precio = new TextView[sitesList5101.getPrecio().size()];
    variacion = new TextView[sitesList5101.getVariacion().size()];
    ttransado = new TextView[sitesList5101.getTtransado().size()];
    pcierre = new TextView[sitesList5101.getPcierre().size()];
    utrans = new TextView[sitesList5101.getUtrans().size()];

    for(int i = 0; i < sitesList5101.getRegistro().size(); i++){

        nemotecnico[i] = new TextView(this);
        nemotecnico[i].setText(" "+ sitesList5101.getNemotecnico().get(i));
        nemotecnico[i].setTextColor(Color.parseColor("#000000"));
        nemotecnico[i].setTypeface(null, Typeface.BOLD);


        precio[i] = new TextView(this);
        precio[i].setText(" "+ sitesList5101.getPrecio().get(i));
        precio[i].setTextColor(Color.parseColor("#333333"));
        precio[i].setTypeface(null, Typeface.BOLD);


        variacion[i] = new TextView(this);
        variacion[i].setText(" "+ sitesList5101.getVariacion().get(i));
        variacion[i].setTextColor(Color.parseColor("#000000"));
        variacion[i].setTypeface(null, Typeface.BOLD);


        ttransado[i] = new TextView(this);
        ttransado[i].setText(" "+ sitesList5101.getTtransado().get(i));
        ttransado[i].setTextColor(Color.parseColor("#333333"));
        ttransado[i].setTypeface(null, Typeface.BOLD);


        pcierre[i] = new TextView(this);
        pcierre[i].setText(" "+ sitesList5101.getPcierre().get(i));
        pcierre[i].setTextColor(Color.parseColor("#333333"));
        pcierre[i].setTypeface(null, Typeface.BOLD);

        utrans[i] = new TextView(this);
        utrans[i].setText(" "+ sitesList5101.getUtrans().get(i));
        utrans[i].setTextColor(Color.parseColor("#333333"));
        utrans[i].setTypeface(null, Typeface.BOLD);

        nemotecnico[i].setTextSize(10);
        precio[i].setTextSize(11);
        variacion[i].setTextSize(11);
        ttransado[i].setTextSize(11);
        pcierre[i].setTextSize(11);
        utrans[i].setTextSize(11);

        Pattern pattern = Pattern.compile("^([a-z: ]*)?+(\\+?[0-9]+([,\\.][0-9]*)?)$");
        Matcher matcher = pattern.matcher(sitesList5101.getVariacion().get(i));
        if(!matcher.matches())

        variacion[i].setTextColor(Color.parseColor("#ff0000"));
        else
        variacion[i].setTextColor(Color.parseColor("#006633"));

    }

    int cont = sitesList5101.getNemotecnico().size();

    for(int i =0; i<cont; i++){
        tr[i] = new TableRow(this);

        tr[i].addView(nemotecnico[i], new TableRow.LayoutParams(1));
        tr[i].addView(precio[i], new TableRow.LayoutParams(2));
        tr[i].addView(variacion[i], new TableRow.LayoutParams(3));
        tr[i].addView(ttransado[i], new TableRow.LayoutParams(4));
        tr[i].addView(pcierre[i], new TableRow.LayoutParams(5));


        layout.addView(tr[i], new TableLayout.LayoutParams());

    }
}

/*********  AlertDialog *********************************************/
/*********  AlertDialog *********************************************/

  public boolean isOnline(Context c) {
        ConnectivityManager cm = (ConnectivityManager) c
        .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();

        if (ni != null && ni.isConnected())
          return true;
        else
          return false;
        }

    @Override
    protected Dialog onCreateDialog(int id) {
      Dialog dialog = null;
      switch (id) {
      case DIALOG_ERROR_CONNECTION:
        AlertDialog.Builder errorDialog = new AlertDialog.Builder(this);
        errorDialog.setTitle("Problema de Conexión");
        errorDialog.setMessage("No tiene conexión a internet, es necesario para acceder al contenido");
        errorDialog.setNeutralButton("ACEPTAR",
        new DialogInterface.OnClickListener() {


          public void onClick(DialogInterface dialog, int id) {
            finish();
          }
        });

       AlertDialog errorAlert = errorDialog.create();
       return errorAlert;

      default:
        break;
      }
    return dialog;
    }

    /*********  AlertDialog *********************************************/
    /*********  AlertDialog *********************************************/

public void refrescar8(View view){
    Button button = (Button)findViewById(R.id.button1);

    if (!isOnline(this)) {
        if(view==button)
            button.setBackgroundResource(R.drawable.buttonrefresh7);
      showDialog(DIALOG_ERROR_CONNECTION); //displaying the created dialog.
    } else {
        if(view==button)
            button.setBackgroundResource(R.drawable.buttonrefresh7);
    onRestart();
    }
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    Intent i = new Intent(xml5101.this, xml5101.class);
    startActivity(i);
    finish();
}

}

edwin rojas
  • 139
  • 2
  • 9
  • in android emulator does not send errors, is when I try it on the cell phone,but I think it's only when I minimize, internet connection and I tried the same thing happens, the screen stays black – edwin rojas Sep 27 '12 at 19:37
  • If you are disabling network connection, there is possibility of many things like in your case the data is not retrieved completely or may be inconsistent data... and you might get `NullPointerExceptoin` at `nemotecnico = new TextView[sitesList5101.getNemotecnico().size()];` So you need to consider all the scenarios and make effective use of `try catch` blocks and `null` handling. **Also make habit of providing us with the stack traces if you get into situations like this, as it will help us to help you better** :) – Vishal Vyas Sep 27 '12 at 19:39

1 Answers1

0

If you turn off GSM, is there a WIFI connection or no connection to the internet? If no internet-connection, accessing the URL will fail and an exception is thrown, which you catch and print to stdout. Have you checked the LogCat output?

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • in android emulator does not send errors,but I think it's only when I minimize, internet connection and I tried the same thing happens, the screen stays black – edwin rojas Sep 27 '12 at 19:38