【问题标题】:How to prevent my app from crashing when handling any error? [duplicate]处理任何错误时如何防止我的应用程序崩溃? [复制]
【发布时间】:2018-03-28 06:56:02
【问题描述】:

我使用OnClickListener 以这种方式执行方法:

    b= (Button) findViewById(R.id.b);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (on){
                try {sendData("b");} catch (IOException e) {e.printStackTrace();}
            }
            else {Toast.makeText(getApplicationContext(), "You should turn bluetooth on!", Toast.LENGTH_SHORT).show();}
        }
    });

sendData 方法:

public void sendData(String s) throws IOException {
    try {
        byte[] byteString = s.getBytes();
        outputStream.write(byteString);
        outputStream.flush();
        Toast.makeText(getApplicationContext(), s + "sent", Toast.LENGTH_SHORT).show();
    }catch (IOException e) {
        Toast.makeText(getApplicationContext(), s + "not sent", Toast.LENGTH_SHORT).show();
        e.printStackTrace();}
}

如果on = true,这会使我的应用程序崩溃

03-28 08:37:09.733 28841-28841/com.android.map.brasrobotique E/AndroidRuntime: 致命异常: main 进程:com.android.map.brasrobotique,PID:28841 java.lang.NullPointerException 在 com.android.map.brasrobotique.MainActivity.sendData(MainActivity.java:221) 在 com.android.map.brasrobotique.MainActivity$2.onClick(MainActivity.java:77) 在 android.view.View.performClick(View.java:4633) 在 android.view.View$PerformClick.run(View.java:19270) 在 android.os.Handler.handleCallback(Handler.java:733) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:146) 在 android.app.ActivityThread.main(ActivityThread.java:5602) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在 dalvik.system.NativeStart.main(Native Method)

如何防止它退出?

谢谢!

【问题讨论】:

  • 请发布错误日志
  • 你应该阅读nested try-catch。如果您需要任何帮助,还可以添加崩溃日志。
  • 而不是 IOException ,使用异常。你没有捕捉到空指针异常。
  • Jyoti,您应该将其发布为答案,这样您就可以获得 15 分!
  • @JyotiJK 非常感谢!!!这拯救了我的一天!!!

标签: java android error-handling


【解决方案1】:

试试:

b= (Button) findViewById(R.id.b);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (on){
                    try {sendData("b");} catch (IOException | NullPointerException e) {e.printStackTrace();}
                }
                else {Toast.makeText(getApplicationContext(), "You should turn bluetooth on!", Toast.LENGTH_SHORT).show();}
            }
        });

这将捕获NullPointerExceptionIOException

【讨论】:

    【解决方案2】:

    你只是在捕捉IOExceptions。抓住任何Exception,它应该可以工作。我怀疑你的outputStream 是空的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 2019-02-03
      • 2020-07-24
      相关资源
      最近更新 更多