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

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

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

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

Android Studio

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

Q&A

解決済

2回答

349閲覧

if or switch文 null状態で実行ボタンを押すとエラー(android studio)

tome_zou

総合スコア6

Java

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

Android Studio

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

0グッド

0クリップ

投稿2018/06/27 07:16

編集2018/06/27 08:14

初心者です、教えていただけないでしょうか。。。

下記のサイトを参考に
(http://blog.livedoor.jp/androidos233/archives/21461371.html)
コピペ改造でアプリ作り、if文やswich文でも無事動いたのですが、何も入力しないnull状態(?)で実行ボタンを押すと、エラーでアプリが落ちてしまいます。
自分の改良が悪いのかと、いろいろ調べたのですが、初心者なのでよく分かりませんでした。

そこで参考にしたサイトのコードをそのままコピペで打ち込んでみたら、何も入力しない(null状態?)で実行ボタンを押すと同様に落ちてしまいました。

サイト自体が2012年と少し古いこともあり、サイトも移設途中で止まっているようで、誰に質問していいか困ってしまい、投稿しました。
どうか対応策をご教授願えないでしょうか?
よろしくお願い致します。

コードは改良したものです。。。

java

1import android.content.Intent; 2import android.support.v7.app.AppCompatActivity; 3import android.os.Bundle; 4import android.view.View; 5import android.widget.Button; 6import android.widget.EditText; 7import android.widget.Toast; 8 9public class MainActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 //ボタン隠す 17 findViewById(R.id.Idp2_button2).setVisibility(View.INVISIBLE); 18 19 //ボタンの情報を取得 20 Button button = (Button) findViewById(R.id.Idp1_button1); 21 button.setOnClickListener(new View.OnClickListener() { 22 23 24 //ボタンがクリックすされた時 25 @Override 26 public void onClick(View v) { 27 Button button = (Button) v; 28 EditText editText = (EditText) findViewById(R.id.editText1); 29 editText.selectAll(); 30 String text = editText.getText().toString(); 31 int text_num = Integer.valueOf(text); 32 33 switch (text_num ){ 34 case 35: 35 findViewById(R.id.Idp2_button2).setVisibility(View.VISIBLE); break; 36 37 default: 38 Toast.makeText(MainActivity.this, "条件と一致しません。", 39 Toast.LENGTH_SHORT).show(); 40 findViewById(R.id.Idp2_button2).setVisibility(View.INVISIBLE);break; 41 } 42 43 } 44 }); 45 } 46 47 // クリック処理 ページ移動 48 public void Idp2_button2(View v) { 49 Intent intent = new Intent(this, Main2Activity.class); // 画面指定 50 startActivity(intent); // 画面を開く 51 52 } 53} 54コード

xml

1<?xml version="1.0" encoding="UTF-8" ?> 2 3<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/Idp1_button1" 12 android:layout_width="wrap_content" 13 android:layout_height="47dp" 14 android:layout_centerHorizontal="true" 15 android:layout_centerVertical="true" 16 android:layout_marginLeft="8dp" 17 android:layout_marginStart="8dp" 18 android:layout_marginTop="8dp" 19 android:background="#66f4dc08" 20 android:text="実行" 21 app:layout_constraintStart_toEndOf="@+id/editText1" 22 app:layout_constraintTop_toTopOf="parent" /> 23 24 <EditText 25 android:id="@+id/editText1" 26 android:layout_width="wrap_content" 27 android:layout_height="44dp" 28 android:layout_above="@+id/Idp1_button1" 29 android:layout_centerHorizontal="true" 30 android:layout_marginLeft="16dp" 31 android:layout_marginStart="16dp" 32 android:layout_marginTop="8dp" 33 android:background="#c1edeb" 34 android:ems="10" 35 android:inputType="number" 36 android:textSize="18sp" 37 app:layout_constraintStart_toStartOf="parent" 38 app:layout_constraintTop_toTopOf="parent" /> 39 40 <Button 41 android:id="@+id/Idp2_button2" 42 android:layout_width="0dp" 43 android:layout_height="wrap_content" 44 android:layout_marginEnd="8dp" 45 android:layout_marginLeft="8dp" 46 android:layout_marginRight="8dp" 47 android:layout_marginStart="8dp" 48 android:layout_marginTop="16dp" 49 android:onClick="Idp2_button2" 50 android:text="\n次のページへ\n" 51 app:layout_constraintEnd_toEndOf="parent" 52 app:layout_constraintHorizontal_bias="0.0" 53 app:layout_constraintStart_toStartOf="parent" 54 app:layout_constraintTop_toBottomOf="@+id/editText1" /> 55 56</android.support.constraint.ConstraintLayout> 57コード

コード

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

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

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

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

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

m.ts10806

2018/06/27 07:24

```java のように書くと言語にあわせて適切にハイライトしてくれるのでブロックの中に(java)のように書く必要はありません。 使える言語一覧→https://teratail.com/questions/7119
m.ts10806

2018/06/27 07:35

あれ?何か伝わってないような・・・。```java とするのは冒頭の```です。
tome_zou

2018/06/27 08:03

すいません、編集しました。
tome_zou

2018/06/27 08:15

すいません、再編集致しました。本当に申し訳あけありません
swordone

2018/06/27 10:27

エラーの際に、なにかログが出ていませんか?
tome_zou

2018/06/27 15:20

イベントログには何も標記がないですが、先ほど、別の方が修正コードを作成していただき解決しました。いろいろ気を掛けていただきありがとうございます。また投稿方法何も分かってなくて申し訳ありませんでした。ご迷惑おかけいたしました。フォーローいたしましたので、今後ともよろしくお願いいたします。
guest

回答2

0

ベストアンサー

「text」が数値では無い時は0が入る様にしてみましたが、
こんなイメージでしょうか?
(ページ移動はToastで代用しました)

Java

1import android.content.Intent; 2import android.support.v7.app.AppCompatActivity; 3import android.os.Bundle; 4import android.util.Log; 5import android.view.View; 6import android.widget.Button; 7import android.widget.EditText; 8import android.widget.Toast; 9 10public class MainActivity extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 17 //ボタン隠す 18 findViewById(R.id.Idp2_button2).setVisibility(View.INVISIBLE); 19 20 //ボタンの情報を取得 21 Button button = findViewById(R.id.Idp1_button1); 22 button.setOnClickListener(new View.OnClickListener() { 23 24 //ボタンがクリックされた時 25 @Override 26 public void onClick(View v) { 27Log.d("debug","Click"); 28 //Button button = (Button) v; 29 EditText editText = findViewById(R.id.editText1); 30 editText.selectAll(); 31 String text = editText.getText().toString(); 32Log.d("debug","Text:" + text); 33 int text_num; 34 if(isNumeric(text)) 35 text_num = Integer.valueOf(text); 36 else 37 text_num = 0; 38Log.d("debug","Num:" + text_num); 39 40 switch (text_num ){ 41 case 35: 42 findViewById(R.id.Idp2_button2).setVisibility(View.VISIBLE); break; 43 44 default: 45 Toast.makeText(MainActivity.this, "条件と一致しません。", 46 Toast.LENGTH_SHORT).show(); 47 findViewById(R.id.Idp2_button2).setVisibility(View.INVISIBLE);break; 48 } 49 } 50 }); 51 } 52 53 public static boolean isNumeric(String value) { 54 try { 55 Double.parseDouble(value); 56 } catch (NumberFormatException e) { 57 return false; 58 } 59 return true; 60 } 61 62 // クリック処理 ページ移動 63 public void Idp2_button2(View v) { 64// Intent intent = new Intent(this, Main2Activity.class); // 画面指定 65// startActivity(intent); // 画面を開く 66 Toast.makeText(MainActivity.this,"画面を開く",Toast.LENGTH_SHORT); // 代用 67 68 } 69}

投稿2018/06/27 08:46

編集2018/06/27 09:38
Wind

総合スコア442

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

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

tome_zou

2018/06/27 15:16

ありがとうございます!! 帰宅後に確認させていただきました。 うれしくて泣きそうです。 後で、しっかりコード内容も確認して勉強します。 本当にありがとうございました!!
guest

0

int text_num = Integer.valueOf(text);

何も入力されていなければ「text」の内容は空文字になるので、ここでNumberFormatExceptionが発生しているのでしょうが。。。

投稿2018/06/27 07:30

tkturbo

総合スコア5572

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

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

tome_zou

2018/06/27 08:02

原因は(text)の部分ですか、いくつか別な文字?を入力してみましたが、そもそもエラーになってしまいますね。 単に文字を入れて正解であれば画面遷移するようにしたかっただけなのですが、、(泣)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問