ボタンを押すとランダムに画像を切り替えるものを作成したいのですがうまくいきません。 エラーの中を見るとランダムで画像ファイル名が選択されていることが確認できるためどう考えていけばわかりません。 どう考えていけばよいか教えていただけないでしょうか。
import android.annotation.SuppressLint import android.graphics.drawable.Drawable import android.os.Bundle import android.util.Log import android.view.View import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* import kotlin.math.floor class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Log.i("MainActivity", "onCreate") val btClick = findViewById<Button>(R.id.btClick) val listener = HelloListener() btClick.setOnClickListener(listener) val iv1 = findViewById<ImageView>(R.id.iv1) } private inner class HelloListener : View.OnClickListener { @SuppressLint("Recycle") override fun onClick(v: View?) { val i = Log.i("MainActivity", "onClick") val gazouArray = resources.obtainTypedArray(R.array.gazou) val rand = floor(Math.random() * 5).toInt() val drawable: Drawable? = gazouArray.getDrawable(rand); iv1.setImageDrawable(drawable) } } }
2020-06-01 17:47:14.062 30035-30035/jp.co.stv_tech.test213 E/AndroidRuntime: FATAL EXCEPTION: main
Process: jp.co.stv_tech.test213, PID: 30035
android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID #0x0
Caused by: android.content.res.Resources$NotFoundException: File money_2 from drawable resource ID #0x0
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:847)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:631)
at android.content.res.Resources.loadDrawable(Resources.java:897)
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:955)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
at jp.co.stv_tech.test213.MainActivity$HelloListener.onClick(MainActivity.kt:38)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.io.FileNotFoundException: money_2
at android.content.res.AssetManager.nativeOpenNonAsset(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:857)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:836)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:631)
at android.content.res.Resources.loadDrawable(Resources.java:897)
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:955)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
at jp.co.stv_tech.test213.MainActivity$HelloListener.onClick(MainActivity.kt:38)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
回答1件
あなたの回答
tips
プレビュー