-1
當我最小化時,我的應用程序崩潰,關閉3G並返回到應用程序。 該應用程序捕捉來自互聯網(SAX解析器)的XML信息,然後按主頁按鈕,關閉3G,並返回到應用程序看起來黑色。當我最小化並返回到應用程序時,應用程序崩潰屏幕變黑
感謝您的幫助
我主要的代碼
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();
}
}
你有什麼錯誤嗎? – MillyMonster
在Android模擬器不發送錯誤,是當我嘗試在手機上,但我認爲這只是當我最小化,互聯網連接和我試圖發生同樣的事情,屏幕保持黑色 –
如果您禁用網絡連接,在你的情況下,可能有很多東西不能完全檢索數據,或者可能是不一致的數據......並且你可能會在'nemotecnico = new TextView [sitesList5101.getNemotecnico()。size()];''NomPointerExceptoin'處得到' 所以你需要考慮所有的場景,並有效地使用'try catch'塊和'null'處理。 **也習慣於爲我們提供堆棧痕跡,如果你遇到這樣的情況,因爲它會幫助我們更好地幫助你** :)在Android模擬器中 –