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

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

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

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

Android Studio

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

Kotlin

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

Q&A

解決済

1回答

1921閲覧

XML&Kotlinにて、ボタンを押すごとにランダムで数字が変わるアプリを制作中に大量のエラー発生

jessy_iida

総合スコア5

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2021/02/07 19:12

編集2021/02/07 21:53

実現したいこと

素数のクイズアプリを作りたい。
第一段階として、ボタンを押したら数字がランダムで出題されるようにしたい

困っていること

runして、仮装デバイスからアプリを開こうとすると大量のエラーが吐き出される
そもそもアプリを開くことができない
大量のエラーの読み方がわからず呆然としてしまい、ここで質問を投げさせていただきます

エラーの状態

アプリを開く前↓

02/08 04:03:04: Launching 'app' on Pixel 3 API 29. Install successfully finished in 1 s 46 ms. $ adb shell am start -n "app.iida.jessy.primequiz/app.iida.jessy.primequiz.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER

アプリを開いたあと↓

02/08 04:06:16: Launching 'app' on Pixel 3 API 29. App restart successful without requiring a re-install. $ adb shell am start -n "app.iida.jessy.primequiz/app.iida.jessy.primequiz.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Connected to process 8808 on device 'emulator-5554'. Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page. W/RenderThread: type=1400 audit(0.0:55): avc: denied { write } for name="property_service" dev="tmpfs" ino=180 scontext=u:r:untrusted_app:s0:c139,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 D/libEGL: Emulator has host GPU support, qemu.gles is set to 1. W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied) D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: app.iida.jessy.primequiz, PID: 8808 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{app.iida.jessy.primequiz/app.iida.jessy.primequiz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174) at android.content.Context.obtainStyledAttributes(Context.java:738) at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806) at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630) at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223) at app.iida.jessy.primequiz.MainActivity.<init>(MainActivity.kt:21) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) at android.app.Instrumentation.newActivity(Instrumentation.java:1243) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

実際のコード

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:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/textView" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="0" 14 android:textSize="64sp" 15 app:layout_constraintBottom_toBottomOf="parent" 16 app:layout_constraintEnd_toEndOf="parent" 17 app:layout_constraintStart_toStartOf="parent" 18 app:layout_constraintTop_toTopOf="parent" /> 19 20 <LinearLayout 21 android:id="@+id/linearLayout" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:animateLayoutChanges="true" 25 android:onClick="chage" 26 android:orientation="horizontal" 27 android:paddingBottom="0dp" 28 app:layout_constraintBottom_toBottomOf="parent" 29 tools:layout_editor_absoluteX="0dp"> 30 31 <Button 32 android:id="@+id/button2" 33 android:layout_width="wrap_content" 34 android:layout_height="96dp" 35 android:layout_weight="1" 36 android:text="変更" 37 android:textSize="64sp" /> 38 </LinearLayout> 39 40</androidx.constraintlayout.widget.ConstraintLayout>

Kotline

1package app.iida.jessy.primequiz 2 3import android.graphics.Color 4import androidx.appcompat.app.AppCompatActivity 5import android.os.Bundle 6import android.util.Log 7import java.util.* 8import android.widget.TextView 9 10 11class MainActivity : AppCompatActivity() { 12 13 companion object { 14 private const val QUESTION_COUNT = 10 15 } 16 17 var random: Random = Random() 18 val question: IntArray = IntArray(QUESTION_COUNT) 19 var point: Int = 0 20 var answerCount: Int = 0 21 ~~var textView = findViewById<TextView>(R.id.textView)~~ 22 23 override fun onCreate(savedInstanceState: Bundle?) { 24 super.onCreate(savedInstanceState) 25 setContentView(R.layout.activity_main) 26 27 var textView = findViewById<TextView>(R.id.textView) 28 29 for (i in 0 until QUESTION_COUNT){ 30 val number = random.nextInt(1000) 31 Log.d("Number","Question"+ number.toString()) 32 question[i] = number 33 } 34 35 point = 0 36 answerCount = 0 37 38 textView.text = question[answerCount].toString() + "" 39 textView.setTextColor(Color.BLACK) 40 41 42 } 43} 44

考え

今回はエラーの感じをみているとXMLの方で問題が起こっている?
ButtonのOnClickにchangeを入れているのが良くない?→抜いてみたけど変わりなし
ここで行き詰まってしまいました。

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

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

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

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

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

hoshi-takanori

2021/02/07 19:42 編集

findViewById は onCreate メソッドの中で setContentView の後に呼び出す必要があります。
jessy_iida

2021/02/07 19:51 編集

なるほど!! 記入を直したら、まずはデバイスが起動できるようになりました! (現在は起動するたびに数字が変わる仕様になっていますが????) 文を各場所一つであんなにエラーが出るんですね・・・ 次に得たーが出た時は、内容だけでなく位置も気をつけて見るようにします!
m.ts10806

2021/02/07 21:16

Ktoline → Kotlin
jessy_iida

2021/02/07 21:54

@m.ts10806 タイトルですね!ほんまですね! お恥ずかしいばかりです。 ご指摘ありがとうございます!
guest

回答1

0

ベストアンサー

もし英語が苦手だというのならGoogle翻訳掛けるだけでも大分読めると思いますが、いかがでしょうか?
こういう初動が起こせるかどうかでも随分分からないと思い込んでいる部分が緩和すると思います。

log

1デバイス「emulator-5554」のプロセス8808に接続されています。 2アプリケーションからlogcatメッセージをキャプチャして表示します。この動作は、「デバッガー」設定ページの「Logcat出力」セクションで無効にできます。 3W / RenderThread:type = 1400 audit(0.0:55):avc:denied {write} for name = "property_service" dev = "tmpfs" ino = 180 scontext = u:r:untrusted_app:s0:c139、c256、c512、 c768 tcontext = u:object_r:property_socket:s0 tclass = sock_file permissive = 0 4D / libEGL:エミュレーターはホストGPUをサポートしており、qemu.glesは1に設定されています。 5W / libc:プロパティ「qemu.gles」を「1」に設定できません:接続に失敗しました。 errno = 13(許可が拒否されました) 6D / libEGL:ロードされた/vendor/lib/egl/libEGL_emulation.so 7D / libEGL:ロードされた/vendor/lib/egl/libGLESv1_CM_emulation.so 8D / libEGL:ロードされた/vendor/lib/egl/libGLESv2_emulation.so 9D / AndroidRuntime:VMのシャットダウン 10E / AndroidRuntime:致命的な例外:メイン 11 プロセス:app.iida.jessy.primequiz、PID:8808

スタックトレース部分は、1行目にメッセージが来るので大抵は翻訳して読めます。
Caused Byは例外エラーオブジェクトの中に別の例外エラーオブジェクトを内包させたものの1行目なので、同様に読めます。

log

1java.lang.RuntimeException:アクティビティをインスタンス化できませんComponentInfo {app.iida.jessy.primequiz / app.iida.jessy.primequiz.MainActivity}:java.lang.NullPointerException:仮想メソッド 'android.content.pm.ApplicationInfoを呼び出そうとしました nullオブジェクト参照のandroid.content.Context.getApplicationInfo() '

log

1原因:java.lang.NullPointerException:nullオブジェクト参照で仮想メソッド 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()'を呼び出そうとしました

これらの情報を合わせると、RuntimeExceptionなのでプログラムの実行時に、
android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()
呼び出そうとしたら**nullが入っているオブジェクトのメソッドを呼んでしまった**。

というのがJVMから見た時の事実なわけですね。
とりあえず、スタックトレースは1行目を頑張って読む
次に2行目以降で自分が触ったソースが無いかを見ていくのが基本です。
自身が書いたソースが全く存在しない場合は、ライブラリ等の設定ファイルとか利用方法が間違っているとかそういう方向に対策の方針を切り替えたりします。

頑張ってください。

投稿2021/02/07 23:16

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問