java - 将致命碰撞变成非致命碰撞分析报告

标签 java android nullpointerexception crash fatal-error

有问题的代码:

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 报告,但不会终止该应用。

最佳答案

你不必抛出任何捕获的异常,你可以像这样很容易地记录它们:

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

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

关于java - 将致命碰撞变成非致命碰撞分析报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25143120/

相关文章:

java - Apache POI (excel) 执行 getSheet() 与 createSheet()

java - 用不同的字符替换字符串中的所有字符

android - 设计应用程序时使用哪种屏幕分辨率?

android - 如何延迟初始化 UI 组件

java - ContextWrapper.getFilesDir() 出现 NullPointerException 的原因

android - 膨胀后自定义 View 的字段为空 - 数据绑定(bind)

java - 如何将TextureRegion转换为Texture以设置 Sprite 的纹理

java - 无法调用链码名称 :"lscc",错误 : transaction returned with failure: Undefined contract method called

javascript - Android webview 不加载移动应用程序中的 URL

nullpointerexception - 从java中的selenium的另一个类调用test