0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Androidアプリ起動、遷移後に落ちる理由

Last updated at Posted at 2021-11-10

Androidアプリを触ってて落ちる原因になりそうなことをちょっこちょこ書いていく忘備録として使います。

onCreateの前にViewの呼び出し

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)
    }

Activityを作成するときにこれ最初からあると思うんですが、これの前にViewの呼び出しをすると落ちます。

OKな例

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)

        val listView = findViewById<ListView>(R.id.list_view)
    }

NGの例

    override fun onCreate(savedInstanceState: Bundle?) {

        val listView = findViewById<ListView>(R.id.list_view)

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)
    }

なぜか落ちると思ってデバックしていろいろやった結果これでした

リファクタリングして綺麗にしようと思った時に結構起きると思うので注意が必要です。

AndroidManifestでエラーが起きてる

クラス名微妙だなと思って変更したタイミングで結構起こりやすいです。

あれ、、、ビルドできるしソース間違えてないのにこの画面の時だけ落ちる、、、みたいな時はAndroid Manifestをチェックしてみましょう。

僕はこのパターンで3時間ほど考え込んでしまいました。

ついでにソースのリファクタリングもしてたので、それもとに戻したりとすごい時間を浪費しました。

その他は都度都度書いていきます。

0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?