初心者です、教えていただけないでしょうか。。。
下記のサイトを参考に
(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コード
コード
回答2件
あなたの回答
tips
プレビュー