###前提・実現したいこと
Android Studioを使ってアプリを作っています。ボタンをタップしたら、Spinnerを含んだアラートダイアログが出て、選択されたものを出力するようなものを作りたいのですが、何が選択されたのかを取得しようとしたらアプリが落ちてしまいました。改善方法を教えて下さい。
また、選択しようとすると、全画面表示が解除されてしまうのも改善方法があれば教えてください。
###エラーメッセージ
09-24 19:12:46.774 28665-28665/com.test.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.test.myapplication, PID: 28665 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.Spinner.getSelectedItem()' on a null object reference at com.test.myapplication.MainActivity$1.onClick(MainActivity.java:41) at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:241) at android.app.ActivityThread.main(ActivityThread.java:6223) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
###該当のソースコード
Java
1package com.test.myapplication; 2 3import android.app.Activity; 4import android.content.DialogInterface; 5import android.support.v7.app.AlertDialog; 6import android.os.Bundle; 7import android.view.LayoutInflater; 8import android.view.View; 9import android.view.ViewGroup; 10import android.view.WindowManager; 11import android.widget.Spinner; 12import android.widget.TextView; 13 14public class MainActivity extends Activity { 15 16 @Override 17 protected void onResume() { 18 super.onResume(); 19 View decor = this.getWindow().getDecorView(); 20 decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 21 | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 22 } 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 } 29 30 public void Button(View view) { 31 LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 32 final View layout = inflater.inflate(R.layout.spinner, (ViewGroup) findViewById(R.id.alertdialog_layout)); 33 34 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 35 alertDialogBuilder.setTitle("Title"); 36 alertDialogBuilder.setView(layout); 37 alertDialogBuilder.setPositiveButton("OK", 38 new DialogInterface.OnClickListener() { 39 public void onClick(DialogInterface dialog, int which) { 40 Spinner spinner = (Spinner) findViewById(R.id.spinner); 41 String item = (String) spinner.getSelectedItem(); 42 ((TextView) findViewById(R.id.text)).setText(item); 43 } 44 }); 45 alertDialogBuilder.setNegativeButton("キャンセル", 46 new DialogInterface.OnClickListener() { 47 public void onClick(DialogInterface dialog, int which) { 48 } 49 }); 50 alertDialogBuilder.setCancelable(true); 51 AlertDialog alertDialog = alertDialogBuilder.create(); 52 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); 53 alertDialog.show(); 54 } 55}
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.test.myapplication.MainActivity"> 7 8 <TextView 9 android:id="@+id/text" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" /> 12 13 <Button 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_centerHorizontal="true" 17 android:layout_centerVertical="true" 18 android:onClick="Button" 19 android:text="Button" /> 20 21</RelativeLayout>
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/alertdialog_layout" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:paddingLeft="20dp" 8 android:paddingTop="20dp"> 9 10 <TextView 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Select" /> 14 15 <Spinner 16 android:id="@+id/spinner" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:entries="@array/list" /> 20</LinearLayout>
xml
1<resources> 2 <string name="app_name">My Application</string> 3 <string-array name="list"> 4 <item>Select1</item> 5 <item>Select2</item> 6 </string-array> 7</resources>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。