需要一些解释

时间:2012-03-09 23:26:42

标签: android try-catch indexoutofboundsexception

所以这是编程期间发生在我身上的最奇怪的事情。是的,我没有专业的编程,但我即时学习。我有一个应用程序与服务器通信,主线程中的套接字,读取是在一个单独的类和线程中完成的,并使用asynctask在一个单独的类中编写。

问题是LocationManager。我可以很好地与服务器和写/读命令对话,我实现了LocationManager及其监听器。

然后我继续实现一个方法,用locatinChanged上的新坐标更新我的textview。到现在为止还挺好。事情就是当我在eclipse中使用Emulator控件并发送坐标时,应用程序崩溃了一个stringOutOfBoundsException(我已经编程了3年,现在从未见过这个)。我看着代码穿过它等等。阅读堆栈跟踪,logcat,控制台以及我能想到的任何地方,但它让我无处可去。直到我终于去了readerthread,看起来像这样:

    public class ReaderThread extends Thread {
    public void run() {
        new Thread(new Runnable(){
            public void run(){                  
                try {
                    //Establish a bufferedreader to read from the socket/server.
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()), 8 * 1024);
            } 
            catch(Exception e) { e.printStackTrace(); }

        //As long as connect is true.       
        while (connected) {
            String line;
            try {
                //Try to read a line from the reader.
                line = in.readLine();
                System.out.println(in.readLine());
                if (in == null) {
                    //No one has sent a message yet.
                    System.out.println("No data recieved");
                }

                else {
                    int i = 0;
                    //As long as someone is sending messages.
                    while((line = in.readLine()) != null ){
                         //Make a new Message.
                        Message msg;
                        msg = new Message();
                        //Set the object to the input line.
                        msg.obj = line;
                        //Set an id so it can be identified in the main class and used in a switch.
                        msg.what = i;
                        System.out.println("i is: "+i);
                        //Send the message to the handler.
                        Main.this.h.sendMessage(msg);   
                    }
                }
            }
            catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

            }           
        }).start(); 
    }

变量i在if语句中,取决于服务器发送的内容,但我将其删除,因为它与此问题无关。

问题在于抓狂。当catch为IOException时,应用程序崩溃。出于愚蠢的运气,我将其更改为Exception并打印e.message以捕获错误并查看导致错误的原因。事情是这个改变固定了它。如何将IOException切换为普通的Exception来修复这样的问题?

与IOException一样,该程序说:“嘿,你不会发现错误,但没有错误”,但是Exception它说“好了,现在你可以抓住它,所以不好了”。

我的应用程序正在运行,但我无法掌握这一点,为什么以及如何发生这种情况?

2 个答案:

答案 0 :(得分:2)

您实际上是在告诉应用程序捕获基类Exception类。这意味着将捕获任何类型的错误,因为所有异常类都来自该基类型。由于StringOutOfBoundsException不会从IOException下降,因此之前未被捕获且错误未被捕获。您可以尝试以下方法,而不是捕获所有异常:

try {
    // Your code here...
} catch (IOException e) {
    Log.e(TAG, "Caught an IOException!", e);
} catch (StringOutOfBoundsException e) {
    Log.e(TAG, "Caught a string out of bounds Exception!", e);
}

我无法确定实际抛出StringOutOfBoundsException的内容。你可以在if语句中删除你的例子。

答案 1 :(得分:1)

    while (connected) {
        String line;
        try {
            //Try to read a line from the reader.
            line = in.readLine();
            System.out.println(in.readLine());
            if (in == null) {
                //No one has sent a message yet.
                System.out.println("No data recieved");
            }

in == null的测试位于一个有趣的位置。你应该收到一个NullPointerException,如果那个测试要返回true,那么就前面几行调用方法。显然,这段代码有点搞笑。

第二次调用时,您无法从in.readLine()保存返回值。我希望它没有任何有用的东西。 (但是,既然你打印了这一行,你显然想知道它包含哪些数据。)

无论line 是什么(从第一次调用in.readLine()开始),它都会被抛弃;在循环写入此行之前,循环中没有其他东西使用它:

                while((line = in.readLine()) != null ){

此时,您阅读的两行已永远消失。

我不完全确定应该采取什么措施来解决这个问题;如果是我的话,我会非常想用一张纸重新开始,并在没有查看现有代码的情况下勾画出该方法应该做什么,然后将草图与代码进行比较,以查看每个人忽略的情况

相关问题