0

我有一些代码可以连接到互联网并提取数据 - 如果它无法连接到 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... !
4

2 回答 2

1

Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

无论您在哪里使用数组/数组列表,请检查它之前是否为空:

MyObject obj = myArray[0];            // For an array

MyObject obj = myArrayList.get(0);    // For an arraylist

logcat您发布的输出来看,它看起来像是myArray[0]在您的AsyncTask. 使用 AsyncTask 的代码编辑问题。

对于数组,执行以下检查:

if (myArray.length > 0) {

    // go crazy within bounds

}

对于数组列表:

if (myArrayList.size() > 0) {

    // go crazy within bounds

}
于 2013-07-25T21:14:53.687 回答
0

除了@vikram,在你玩数组之前检查

if(yourArray.size() > 0){
  //
}

如果您“即时”更改阵列,请Vector改用(矢量已同步。)以防止出现此问题。

或使用:

List yourArray= Collections.synchronizedList (yourArray);
于 2013-07-25T21:17:31.747 回答