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

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

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

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

Android Studio

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

Q&A

解決済

1回答

1293閲覧

Android Studio アプリ開発

退会済みユーザー

退会済みユーザー

総合スコア0

Java

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

Android Studio

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

0グッド

0クリップ

投稿2017/10/02 09:15

###前提・実現したいこと

ソーテック社発行のこれ一冊できるAndroidStudioアプリ開発入門の手順に沿ってじゃんけんゲームを作っています。
###発生している問題・エラーメッセージ
おそらくonClickで結びつけたメソッドがうまく動かずラジオボタンの選択までは動きますがその後の処理で落ちます。
エミュレータ上でmyfirstapp has stoppedと表示されアプリが落ちてしまいます。
###該当のソースコード

[MainActivity.java] package com.example.kazu.myfirstapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private RadioButton radioGoo; private RadioButton radioChoki; private RadioButton radioPaa; private TextView resultTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGoo = (RadioButton)findViewById(R.id.radioGoo); radioChoki = (RadioButton)findViewById(R.id.radioChoki); radioPaa = (RadioButton)findViewById(R.id.radioPaa); } public void onDuel(View view){ Toast.makeText(this,"私はグー",Toast.LENGTH_SHORT).show(); String resultString = "残念でした"; if(radioGoo.isChecked()){ resultString = "引き分けです"; }else if(radioPaa.isChecked()){ resultString = "おめでとう"; } resultTextView.setText(resultString); } } [activity_main.xml] <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.kazu.myfirstapp.MainActivity"> <TextView android:id="@+id/titleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="18dp" android:layout_marginTop="14dp" android:text="@string/title_janken" android:textAppearance="@android:style/TextAppearance.Material.Large" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.068" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.027" /> <Button android:id="@+id/showResultButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:onClick="onDuel" android:text="@string/button_showsign" android:textSize="22sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.499" /> <RadioGroup android:id="@+id/gooChokiPaaGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/resultTextView" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" android:layout_marginBottom="80dp" android:orientation="horizontal"> <RadioButton android:id="@+id/radioGoo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/goo" /> <RadioButton android:id="@+id/radioChoki" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/choki" /> <RadioButton android:id="@+id/radioPaa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/paa" /> </RadioGroup> <TextView android:id="@+id/resultTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/showResultButton" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" android:text="@string/start_janken" /> </RelativeLayout>

###試したこと
onDuelの内容をトーストのみにした場合、アプリは落ちませんでした。

###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

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

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

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

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

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

guest

回答1

0

ベストアンサー

変数 resultTextView が初期化されていません。
初期化しないままonDuel() で resultTextView.setText(resultString);
するので落ちます。onCreate()で

Java

1 2resultTextView = (TextView)findViewById(R.id.resultTextView); 3

が必要なはずです。※ビルド時に Android Studio で警告が出なかったでしょうか。

投稿2017/10/02 09:25

dodox86

総合スコア9183

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問