我有一些代码可以连接到互联网并提取数据 - 如果它无法连接到 URL - 它会显示一个错误屏幕,但是当它到达该错误屏幕/布局时,用户会收到强制关闭错误,我需要弄清楚如何防止这种情况发生。
资源:
if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {
try {
// updating layout initially has updating text with 1 dot in the
// xml
setContentView(R.layout.updating);
// This image view has the updating text to be progressively
// updated
// with dots addition
ImageView loading = (ImageView) findViewById(R.id.loading_empty1);
// Button goButton = (Button) findViewById(R.id.loading_empty);
// Set updating button to drawable animation
loading.setBackgroundResource(R.drawable.updating1);
loadingAnimation = (AnimationDrawable) loading.getBackground();
ImageView loading2 = (ImageView) findViewById(R.id.loading_empty2);
// Button goButton = (Button) findViewById(R.id.loading_empty);
// Set updating button to drawable animation
loading2.setBackgroundResource(R.drawable.updating2);
loadingAnimation = (AnimationDrawable) loading2.getBackground();
// mProgressImageview = (ImageView)
// findViewById(R.id.updating_text);
// mLoadingCircle = (ImageView)
// findViewById(R.id.updating_hand);
tasks.execute("https://dl.dropboxusercontent.com/u/3177165644456456/GetPhoneSettings-rsp-eng.xml");
if (tasks.get() != null) {
stream = tasks.getInputStream();
Log.v("CfA", "here");
}else {
setContentView(R.layout.error);
//finish();
}
日志猫:
01-01 00:14:28.175: E/AndroidRuntime(5253): FATAL EXCEPTION: AsyncTask #2
01-01 00:14:28.175: E/AndroidRuntime(5253): java.lang.RuntimeException: An error occured while executing doInBackground()
01-01 00:14:28.175: E/AndroidRuntime(5253): at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.lang.Thread.run(Thread.java:1019)
01-01 00:14:28.175: E/AndroidRuntime(5253): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.ArrayList.get(ArrayList.java:311)
01-01 00:14:28.175: E/AndroidRuntime(5253): at com.project.example.datasettings.ConfigFinalActivity.getValues(ConfigFinalActivity.java:355)
01-01 00:14:28.175: E/AndroidRuntime(5253): at com.project.example.datasettings.ConfigFinalActivity.InsertAPN(ConfigFinalActivity.java:324)
01-01 00:14:28.175: E/AndroidRuntime(5253): at com.project.example.datasettings.ConfigFinalActivity.updateTable(ConfigFinalActivity.java:676)
01-01 00:14:28.175: E/AndroidRuntime(5253): at com.project.example.datasettings.ConfigFinalActivity.access$0(ConfigFinalActivity.java:643)
01-01 00:14:28.175: E/AndroidRuntime(5253): at com.project.example.datasettings.ConfigFinalActivity$TableUpdateRequestTask.doInBackground(ConfigFinalActivity.java:548)
01-01 00:14:28.175: E/AndroidRuntime(5253): at com.project.example.datasettings.ConfigFinalActivity$TableUpdateRequestTask.doInBackground(ConfigFinalActivity.java:1)
01-01 00:14:28.175: E/AndroidRuntime(5253): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-01 00:14:28.175: E/AndroidRuntime(5253): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-01 00:14:28.175: E/AndroidRuntime(5253): ... 4 more
01-01 00:14:28.400: D/CLIPBOARD(5253): Hide Clipboard dialog at Starting input: finished by someone else... !