【问题标题】:Turn a fatal crash into a non-fatal crashlytics report将致命的崩溃变成非致命的崩溃分析报告
【发布时间】:2014-09-28 08:52:00
【问题描述】:

有问题的代码:

final GenericAsyncTask task = new GenericAsyncTask();

        task.background = new Runnable() {
            public void run() {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(GCMIntentService.this.host);
                String addremove = "add";
                if(register == false) addremove = "remove";

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
                    nameValuePairs.add(new BasicNameValuePair("cmd", addremove));
                    nameValuePairs.add(new BasicNameValuePair("subscription_key", SUBSCRIPTION_KEY)); // unique per app
                    nameValuePairs.add(new BasicNameValuePair("token", str));
                    nameValuePairs.add(new BasicNameValuePair("os_family", "android"));
                    if(addremove.equals("remove")) nameValuePairs.add(new BasicNameValuePair("hard", "" + hard));
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
                    httppost.setEntity(entity);
                    Log.i(LCHApplication.TAG, "Name Value Pairs: " + nameValuePairs.toString());

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                    task.result = EntityUtils.toString(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        task.callback = new Runnable() {
            public void run() {
                Log.i(LCHApplication.TAG, "registration response: " + task.result.toString());
            }
        };

        task.execute();

这在大多数情况下都非常有效。然而,有时task.result.toString(); 有时是NULL,它会抛出java.lang.NullPointerException。我想保留这个崩溃,因为我希望它在 crashlytics 中报告,这样我就可以看到它影响了多少人。如果我使用 try/catch 我可以抛出什么来确保它不是致命的崩溃呢?这样它仍然会向 crashlytics 报告,但不会杀死应用程序。

【问题讨论】:

    标签: java android nullpointerexception crash fatal-error


    【解决方案1】:

    您不必抛出任何捕获的异常,您可以像这样非常轻松地记录它们:

    try {
      myMethodThatThrows();
    } catch (Exception e) {
      Crashlytics.logException(e);
      // handle your exception here! 
    }
    

    来源:http://support.crashlytics.com/knowledgebase/articles/202805-logging-caught-exceptions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 2015-07-02
      相关资源
      最近更新 更多