java - 应用程序强制关闭,但没有强制关闭消息

标签 java android bufferedreader forceclose

我有点困惑......

我有以下方法,并且我的应用程序在没有强制关闭对话框的情况下关闭。我不知道为什么。我想应该一切都好... 我无法提供更多信息。如果您想要更多,请问我。

public void findCC3000(View view) {
        new AsyncTask<String, Integer, String>() {
            private ProgressDialog dialog;

            protected void onPreExecute() {
                dialog = new ProgressDialog(MainActivity.this);
                dialog.setMax(64516);
                dialog.setCancelable(false);
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                this.dialog.setProgress(0);
                this.dialog.show();
            }

            @Override
            protected String doInBackground(String... strings) {
                int port = Integer.valueOf(((EditText) MainActivity.this.findViewById(R.id.editText_port)).getText().toString());
                String  ip = ((EditText) MainActivity.this.findViewById(R.id.editText_ip)).getText().toString();
                try {
                    out("try to connect");
                socket = new Socket(ip, port);
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                printWriter.println("");



                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String fromServer;

                    out("listen for message");



                while ((fromServer = in.readLine()) != null) { //CRASH-------------------------------------------------------
                    out(fromServer);
                    if (fromServer.equals("Connected to CC3000")) {
                        out("CC3000 found! : " + ip);
                        //startListenThread();
                        out("Started Lissten thread " );
                        findViewById(R.id.button_connect).setEnabled(false);
                        findViewById(R.id.joystickView_geschwindigkeit).setEnabled(true);
                        findViewById(R.id.joystickView_lenkung).setEnabled(true);
                        return ip;
                    }
                }



                } catch (UnknownHostException e) {
                    out(e.getMessage());
                } catch (IOException e) {
                    out(e.getMessage());
                }

                return "";
            }

            protected void onProgressUpdate(Integer... progress) {
                dialog.setProgress(progress[0]);
            }

            protected void onPostExecute(String result) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (result.isEmpty())
                    Toast.makeText(MainActivity.this, "CC3000 not found! :(", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(MainActivity.this, "CC3000 found! :)", Toast.LENGTH_SHORT).show();

                ip = result;
            }
        }.execute("192.168.");
    }

但我知道那个地方。 所以重要的部分是(readLine()):

while ((fromServer = in.readLine()) != null) { //CRASH-------------------------------------------------------
                        out(fromServer);
                        if (fromServer.equals("Connected to CC3000")) {
                            out("CC3000 found! : " + ip);
                            //startListenThread();
                            out("Started Lissten thread " );
                            findViewById(R.id.button_connect).setEnabled(false);
                            findViewById(R.id.joystickView_geschwindigkeit).setEnabled(true);
                            findViewById(R.id.joystickView_lenkung).setEnabled(true);
                            return ip;
                        }
                    }

这是我的 Logcat:

06-06 16:12:16.494 20152-20166/de.mayerhofersimon.cc3000.main W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x4155dce0)

希望能帮到你。

问候

最佳答案

只有 UI 线程可以接触 UI。你可以看一下setEnabled的实现

4586     public void More ...setEnabled(boolean enabled) {
4587         if (enabled == isEnabled()) return;
4588 
4589         setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4590 
4591         /*
4592          * The View most likely has to change its appearance, so refresh
4593          * the drawable state.
4594          */
4595         refreshDrawableState();
4596 
4597         // Invalidate too, since the default behavior for views is to be
4598         // be drawn at 50% alpha rather than to change the drawable.
4599         invalidate(true);
4600     }

关于java - 应用程序强制关闭,但没有强制关闭消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24084319/

相关文章:

android - UIAutomator ScrollToBeginning 不会一直滚动到开始

Java 泛型用例

java - 为什么我不能将 T 类型的对象传递给 < 类型对象的方法?延伸 T>?

java - 如何在一个类中执行多个AsyncTask

Java 数字格式异常

java - 扫描仪不会读取文件中的下一行。 java

while-loop - while 循环中使用的 BufferedReader readLine

java - 在不触发提交按钮的情况下验证编辑文本

java - 如何有效地从 Java 中的 LinkedHashMap 中删除条目?

android - 如何将 APK 发布到 Google 开发者控制台