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>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。