如何防止我的应用程序在处理任何错误时崩溃?

时间:2018-03-28 06:56:02

标签: java android error-handling

我使用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:致命异常:主要                                                                                  处理: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)                                                                                      at 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(本地方法)

如何防止它退出?

谢谢你!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你只是抓住了IOException。抓住任何Exception它应该有效。我怀疑你的outputStream为空。