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

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

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

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

Android

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

Q&A

解決済

3回答

4937閲覧

AndroidでfindViewByIdがnullを返す

退会済みユーザー

退会済みユーザー

総合スコア0

Java

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

Android

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

0グッド

0クリップ

投稿2016/08/11 05:03

編集2016/08/11 06:07

Androidのアプリ開発を始めました。
screenNameのEditTextに入力された内容を取得したいのですが、EditTextを取得するためのfindViewByIdでnullを返してきて動きません。どうすればfindViewByIdでnullではなくscreenNameのEditTextを取得できるでしょうか?

追記(ソースコード)

java

1package tk.surume.android.misqforandroid; 2 3import android.net.Uri; 4import android.support.v7.app.AppCompatActivity; 5import android.os.Bundle; 6import android.view.View; 7import android.widget.EditText; 8import android.widget.Toast; 9 10import com.google.android.gms.appindexing.Action; 11import com.google.android.gms.appindexing.AppIndex; 12import com.google.android.gms.common.api.GoogleApiClient; 13 14public class LoginActivity extends AppCompatActivity { 15 16 /** 17 * ATTENTION: This was auto-generated to implement the App Indexing API. 18 * See https://g.co/AppIndexing/AndroidStudio for more information. 19 */ 20 private GoogleApiClient client; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_login); 26 // ATTENTION: This was auto-generated to implement the App Indexing API. 27 // See https://g.co/AppIndexing/AndroidStudio for more information. 28 client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 29 } 30 31 @Override 32 public void onStart() { 33 super.onStart(); 34 35 // ATTENTION: This was auto-generated to implement the App Indexing API. 36 // See https://g.co/AppIndexing/AndroidStudio for more information. 37 client.connect(); 38 Action viewAction = Action.newAction( 39 Action.TYPE_VIEW, // TODO: choose an action type. 40 "Login Page", // TODO: Define a title for the content shown. 41 // TODO: If you have web page content that matches this app activity's content, 42 // make sure this auto-generated web page URL is correct. 43 // Otherwise, set the URL to null. 44 Uri.parse("http://host/path"), 45 // TODO: Make sure this auto-generated app URL is correct. 46 Uri.parse("android-app://tk.surume.android.misqforandroid/http/host/path") 47 ); 48 AppIndex.AppIndexApi.start(client, viewAction); 49 } 50 51 @Override 52 public void onStop() { 53 super.onStop(); 54 55 // ATTENTION: This was auto-generated to implement the App Indexing API. 56 // See https://g.co/AppIndexing/AndroidStudio for more information. 57 Action viewAction = Action.newAction( 58 Action.TYPE_VIEW, // TODO: choose an action type. 59 "Login Page", // TODO: Define a title for the content shown. 60 // TODO: If you have web page content that matches this app activity's content, 61 // make sure this auto-generated web page URL is correct. 62 // Otherwise, set the URL to null. 63 Uri.parse("http://host/path"), 64 // TODO: Make sure this auto-generated app URL is correct. 65 Uri.parse("android-app://tk.surume.android.misqforandroid/http/host/path") 66 ); 67 AppIndex.AppIndexApi.end(client, viewAction); 68 client.disconnect(); 69 } 70 71 public void onLoginClicked(View v){ 72 EditText screenNameInput = (EditText)findViewById(R.id.screenName); 73 System.out.println(screenNameInput); 74 String screenName = screenNameInput.getText().toString(); 75 Toast.makeText(this, screenName, Toast.LENGTH_LONG).show(); 76 } 77}

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 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="tk.surume.android.misqforandroid.LoginActivity"> 11 12 <EditText 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:id="@+id/screenName" 16 android:layout_alignParentTop="true" 17 android:layout_alignParentStart="true" 18 android:layout_alignParentEnd="true" 19 android:text="*****" 20 android:hint="@string/screen_name" /> 21 22 <EditText 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:inputType="textPassword" 26 android:ems="10" 27 android:id="@+id/password" 28 android:layout_below="@+id/screenName" 29 android:layout_alignParentStart="true" 30 android:layout_alignEnd="@+id/screenName" 31 android:hint="@string/password" 32 android:text="*****" /> 33 34 <Button 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="@string/login" 38 android:id="@+id/button" 39 android:layout_below="@+id/password" 40 android:layout_alignEnd="@+id/password" 41 android:layout_alignParentStart="true" 42 android:onClick="onLoginClicked" /> 43</RelativeLayout>

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

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

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

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

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

guest

回答3

0

ベストアンサー

Androidシミュレータを再起動したらうまく行きました。

投稿2016/08/11 07:40

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

もしかして、Buttonの掛け違いでは

目当てのEditTextを取得できるでしょうか?

2つEditTextがあるので
どれを取得したいか明確に書かないと
これがプログラムだと即エラーです

android:id="@+id/screenName"

は取得できていますが

android:id="@+id/password"

は取り出すコードがないので取得できていないとしか言いようがないのですが
いかがでしょう

投稿2016/08/11 06:06

aja

総合スコア3733

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

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

退会済みユーザー

退会済みユーザー

2016/08/11 06:09

追記しました。取得したいのはscreenNameのtextBoxです。
aja

2016/08/11 07:10

abc と打ち込んでボタンをタップするとToastに abc とでてきますよ App Indexing APIがうまくできていない ということについてはわかりませんが
guest

0

idが正しくないからですね。
読み込んでいるレイアウトファイルの中にidがあるかを確認しましょう。

投稿2016/08/11 05:15

yona

総合スコア18155

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

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

yona

2016/08/11 05:42 編集

補完して得られるidにはSDKで定義されているidやプロジェクト内の他のidも含まれます。 読み込んでいるレイアウトファイルとfindViewByIdを行っているコードを質問に追記してください。
退会済みユーザー

退会済みユーザー

2016/08/11 05:44

追記しました。
yona

2016/08/11 09:45

インスタントランで実行していませんか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問