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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

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

Q&A

解決済

1回答

2646閲覧

コードを追記すると、『繰り返し停止しています。』【Kotlin】

kazuki_user

総合スコア147

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2020/07/29 23:18

下記コードを追記すると、『Kotlin_1(※アプリ名)が繰り返し停止しています。』
と出てしまい、アプリが落ちてしまいます。

Kotlin

1val rollButton = findViewById<Button>(R.id.rollButton) 2 val resultTextView = findViewById<TextView>(R.id.resultsTextView) 3 val seekBar = findViewById<SeekBar>(R.id.seekBar3) 4 5 rollButton.setOnClickListener{ 6 val rand = Random().nextInt(seekBar.progress) 7 resultTextView.text = rand.toString() 8 }

解決策を教えて頂けると嬉しいです。

以下、全体のコードです。


Kt

1package com.example.kotlin_1 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.widget.Button                --------------ここから 6import android.widget.SeekBar 7import android.widget.TextView 8import java.util.*                     --------------ここまで 9 10class MainActivity : AppCompatActivity() { 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 setContentView(R.layout.activity_main) 14 15 val rollButton = findViewById<Button>(R.id.rollButton) --------------ここから 16 val resultTextView = findViewById<TextView>(R.id.resultsTextView) 17 val seekBar = findViewById<SeekBar>(R.id.seekBar3) 18 19 rollButton.setOnClickListener{ 20 val rand = Random().nextInt(seekBar.progress) 21 resultTextView.text = rand.toString() 22 }                                 --------------ここまで 23 } 24} 25

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/rollButton" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 11 <Button 12 android:id="@+id/button1" 13 android:layout_width="0dp" 14 android:layout_height="wrap_content" 15 android:layout_marginStart="24dp" 16 android:layout_marginLeft="24dp" 17 android:layout_marginEnd="24dp" 18 android:layout_marginRight="24dp" 19 android:text="Roll" 20 app:layout_constraintBottom_toBottomOf="parent" 21 app:layout_constraintEnd_toEndOf="parent" 22 app:layout_constraintHorizontal_bias="0.498" 23 app:layout_constraintStart_toStartOf="parent" 24 app:layout_constraintTop_toTopOf="parent" 25 app:layout_constraintVertical_bias="0.911" /> 26 27 <SeekBar 28 android:id="@+id/seekBar3" 29 style="@style/Widget.AppCompat.SeekBar.Discrete" 30 android:layout_width="0dp" 31 android:layout_height="wrap_content" 32 android:max="10" 33 android:progress="3" 34 app:layout_constraintBottom_toTopOf="@+id/button1" 35 app:layout_constraintEnd_toEndOf="parent" 36 app:layout_constraintHorizontal_bias="0.0" 37 app:layout_constraintStart_toStartOf="parent" 38 app:layout_constraintTop_toTopOf="parent" 39 app:layout_constraintVertical_bias="0.93" /> 40 41 <TextView 42 android:id="@+id/textView5" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:layout_marginStart="24dp" 46 android:layout_marginLeft="24dp" 47 android:layout_marginBottom="24dp" 48 android:text="TextView" 49 app:layout_constraintBottom_toTopOf="@+id/seekBar3" 50 app:layout_constraintStart_toStartOf="parent" /> 51 52 <TextView 53 android:id="@+id/resultsTextView" 54 android:layout_width="wrap_content" 55 android:layout_height="wrap_content" 56 android:text="TextView" 57 android:textSize="144sp" 58 app:layout_constraintBottom_toTopOf="@+id/seekBar3" 59 app:layout_constraintEnd_toEndOf="parent" 60 app:layout_constraintStart_toStartOf="parent" 61 app:layout_constraintTop_toTopOf="parent" /> 62</androidx.constraintlayout.widget.ConstraintLayout>

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

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

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

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

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

guest

回答1

0

ベストアンサー

以下の行が誤っているのではないでしょうか。
※「R.id.rollButton」はボタンじゃないのに、ボタンにキャストしようとして失敗しているのではないでしょうか。

Kotlin

1// 修正前 2val rollButton = findViewById<Button>(R.id.rollButton) 3 4// 修正後 5val rollButton = findViewById<Button>(R.id.button1)

↓実行結果
スクリーンショット

投稿2020/07/30 09:02

編集2020/07/30 09:08
tsuki01

総合スコア1751

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問