[英]Android App Force Closes on Error Layout
I have a bit of code which connects to the internet and pulls data - if it is unable to connect to the URL - it displays an error screen however when it reaches that error screen/layout the user recieves a force close error and I need to figure out how to prevent that from happening. 我有一些代码可以连接到互联网并提取数据-如果它无法连接到URL-它会显示一个错误屏幕,但是当到达该错误屏幕/布局时,用户会收到强制关闭错误,我需要弄清楚如何防止这种情况的发生。
SOURCE: 资源:
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();
}
LOGCAT: LOGCAT:
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... !
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
Wherever you are using an array/arraylist, check if it is empty before: 无论您在哪里使用数组/数组列表,都请在以下位置检查它是否为空:
MyObject obj = myArray[0]; // For an array
MyObject obj = myArrayList.get(0); // For an arraylist
From the logcat
output you have posted, it looks like myArray[0]
is in your AsyncTask
. 从您发布的logcat
输出中,看起来myArray[0]
在您的AsyncTask
。 Edit the question with your AsyncTask's code. 使用AsyncTask的代码编辑问题。
For an array, perform the following check: 对于数组,请执行以下检查:
if (myArray.length > 0) {
// go crazy within bounds
}
For an arraylist: 对于数组列表:
if (myArrayList.size() > 0) {
// go crazy within bounds
}
In addition to @vikram, before you play with Arrays check 除了@vikram之外,在使用数组之前请检查
if(yourArray.size() > 0){
//
}
If you change your Array "on the fly", use Vector
instead (Vector is synchronized.), to prevent this issue. 如果您“即时”更改数组,请改用Vector
(Vector已同步。),以防止出现此问题。
Or use: 或使用:
List yourArray= Collections.synchronizedList (yourArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.