|
| 1 | +/* |
| 2 | + * Copyright Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.samples.quickstart.crash |
| 18 | + |
| 19 | +import android.os.Bundle |
| 20 | +import android.support.v7.app.AppCompatActivity |
| 21 | +import android.util.Log |
| 22 | +import android.widget.Button |
| 23 | +import android.widget.CheckBox |
| 24 | + |
| 25 | +import com.google.firebase.crash.FirebaseCrash |
| 26 | + |
| 27 | +/** |
| 28 | + * This Activity shows the different ways of reporting application crashes. |
| 29 | + * - Report caught crashes with Crash.report(). |
| 30 | + * - Automatically Report uncaught crashes. |
| 31 | + * |
| 32 | + * |
| 33 | + * It also shows how to add log messages to crash reports using Crash.log(). |
| 34 | + * |
| 35 | + * |
| 36 | + * Check https://console.firebase.google.com to view and analyze your crash reports. |
| 37 | + * |
| 38 | + * |
| 39 | + * Check https://firebase.google.com/docs/crash/android for more on |
| 40 | + * Firebase Crash on Android. |
| 41 | + */ |
| 42 | +class MainActivity : AppCompatActivity() { |
| 43 | + private val TAG = "MainActivity" |
| 44 | + |
| 45 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 46 | + super.onCreate(savedInstanceState) |
| 47 | + setContentView(R.layout.activity_main) |
| 48 | + |
| 49 | + // Checkbox to indicate when to catch the thrown exception. |
| 50 | + val catchCrashCheckBox = findViewById(R.id.catchCrashCheckBox) as CheckBox? |
| 51 | + |
| 52 | + // Button that causes the NullPointerException to be thrown. |
| 53 | + (findViewById(R.id.crashButton) as Button).setOnClickListener { |
| 54 | + // Log that crash button was clicked. This version of Crash.log() will include the |
| 55 | + // message in the crash report as well as show the message in logcat. |
| 56 | + FirebaseCrash.logcat(Log.INFO, TAG, "Crash button clicked") |
| 57 | + |
| 58 | + // If catchCrashCheckBox is checked catch the exception and report is using |
| 59 | + // Crash.report(). Otherwise throw the exception and let Firebase Crash automatically |
| 60 | + // report the crash. |
| 61 | + when (catchCrashCheckBox?.isChecked) { |
| 62 | + true -> { |
| 63 | + try { |
| 64 | + throw NullPointerException() |
| 65 | + } catch (ex: NullPointerException) { |
| 66 | + // [START log_and_report] |
| 67 | + FirebaseCrash.logcat(Log.ERROR, TAG, "NPE caught") |
| 68 | + FirebaseCrash.report(ex) |
| 69 | + // [END log_and_report] |
| 70 | + } |
| 71 | + } |
| 72 | + false -> { |
| 73 | + throw NullPointerException() |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Log that the Activity was created. This version of Crash.log() will include the message |
| 79 | + // in the crash report but will not be shown in logcat. |
| 80 | + // [START log_event] |
| 81 | + FirebaseCrash.log("Activity created") |
| 82 | + // [END log_event] |
| 83 | + } |
| 84 | +} |
0 commit comments