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

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

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

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Q&A

解決済

2回答

737閲覧

Spinnerの選択されたものの取得について

CoC1216

総合スコア13

Java

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

0グッド

0クリップ

投稿2017/09/24 10:19

###前提・実現したいこと
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>

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

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

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

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

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

guest

回答2

0

ベストアンサー

(Spinner) findViewById(R.id.spinner)を実行する対象がMainActivityです。MainActivityにSpinnerはないためこの結果がnullになり、そのnullに対してgetSelectedItem()しようとしたための例外です。
おそらくですが、findVIewByIdの前にlayout.を付けるといいのではないでしょうか?

投稿2017/09/24 10:34

編集2017/09/24 10:37
swordone

総合スコア20651

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

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

0

ActivityのfindViewByIdが呼ばれています。
Spinnerがあるのはlayoutインスタンスの中なので、layout.findViewByIdを呼ばないといけません。

投稿2017/09/24 10:37

編集2017/09/24 13:09
yona

総合スコア18155

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

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

hiramekun

2017/09/24 12:50

findViewByIdを呼ぶのならViewGroupにキャストする必要はありませんね
yona

2017/09/24 13:08

そうでした、修正します
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問