質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1241閲覧

[Kotlinアプリ開発]ボタンを押すたびにボタンが増える仕様にしたいが、2回目で強制終了する。

tamano54

総合スコア1

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/05/06 12:10

編集2020/05/06 12:12

前提・実現したいこと

AndroidStudioでKotlinのアプリ開発演習をしています。
「CREATE」ボタンを押すたびに、「ITEM」ボタンが増えるアプリを作りたいです。
###発生している問題
「CREATE」ボタンを一度押すと、想定通り「ITEM」ボタンが1個でてくるのですが、2度目にはアプリが強制終了してしまいます。
強制終了せずに、「ITEM」ボタンを増やすためにはどのように修正すれば良いでしょうか?

発生している問題・エラーメッセージ

アプリの強制終了

該当のソースコード

MainActivity.kt

package com.example.tempapplicarion import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.LinearLayout class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val layout = findViewById<LinearLayout>(R.id.linear) val create = findViewById<Button>(R.id.Create) val button = Button(this) button.text = "ITEM" create.setOnClickListener { layout.addView(button) } } }

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/Create" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Create" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <LinearLayout android:id="@+id/linear" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="1dp" android:layout_marginEnd="1dp" android:orientation="vertical" app:layout_constraintBottom_toTopOf="@+id/Create" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"></LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>

試したこと

本文はこちらを参考にしました。
https://qiita.com/shun-shun123/items/7fab7ad3c6b040803f52

補足情報(FW/ツールのバージョンなど)

AndroidStudio3.6.3
Kotlin1.3.71

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Kotlin

1 val button = Button(this) 2 button.text = "ITEM" 3 4 create.setOnClickListener { 5 layout.addView(button) 6 }

Kotlin

1 2 create.setOnClickListener { 3 val button = Button(this) 4 button.text = "ITEM" 5 layout.addView(button) 6 }

としたらできませんか?
どのようなエラー文か分かりませんが、同じインスタンスを2回以上addできないからだと思われます。

投稿2020/05/06 12:17

nakasho_dev

総合スコア2655

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tamano54

2020/05/06 12:24

ご回答ありがとうございます。 お教えいただいた通り修正したら無事にボタンを増やすことができました。 同じインスタンスは繰り返しできないのですね。勉強になりました。 ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問