Android App Force关闭错误布局

时间:2013-07-25 21:11:51

标签: java android layout android-asynctask forceclose

我有一些连接到互联网并提取数据的代码 - 如果它无法连接到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();
            }

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... !

2 个答案:

答案 0 :(得分:1)

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

无论您在何处使用数组/ arraylist,请检查它是否为空:

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

}

对于一个arraylist:

if (myArrayList.size() > 0) {

    // go crazy within bounds

}

答案 1 :(得分:0)

除了@vikram之外,在玩Arrays之前检查

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

如果您“动态”更改数组,请改为使用Vector(Vector已同步。),以防止出现此问题。

或使用:

List yourArray= Collections.synchronizedList (yourArray);