Android EditText giving error on onclick method -
i trying read in contents of w_number_edit_text
(a textbox) after login(a button) clicked. on onclick
method can't seem access contents of w_number_edit_text
. both textbox , button on fragment xml instead of main.xml
. can tell me how fix problem. in short how access contents of edittext
after button
clicked? thank you
code below:
package com.example.medrec; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.app.alertdialog; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.view.window; import android.view.windowmanager; import android.webkit.webview.findlistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; import android.os.build; public class mainactivity extends actionbaractivity { button login; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()) .commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment implements onclicklistener{ public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); button login = (button)rootview.findviewbyid(r.id.w_login); login.setonclicklistener(this); return rootview; } @override public void onclick(view v) { //////////////////////////// problem here edittext wnumbers =(edittext)findviewbyid(r.id.w_number_edit_text); string wnumber = wnumbers.gettext().tostring(); toast.maketext(getactivity(), wnumber, toast.length_long).show(); } } }
my activity_main.xml
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.medrec.mainactivity" tools:ignore="mergerootframe" />
my fragment_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.medrec.mainactivity$placeholderfragment" > <edittext android:id="@+id/w_number_edit_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview1" android:layout_centerhorizontal="true" android:layout_margintop="21dp" android:ems="10" android:inputtype="number" > <requestfocus /> </edittext> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="118dp" android:text="@string/w_number" /> <button android:id="@+id/w_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/w_number_edit_text" android:layout_centerhorizontal="true" android:layout_margintop="15dp" android:text="@string/w_login_text" /> </relativelayout>
you should change this
edittext wnumbers =(edittext)findviewbyid(r.id.w_number_edit_text);
with
edittext wnumbers =(edittext)rootview.findviewbyid(r.id.w_number_edit_text);
and oncreateview(......)
below:
edittext wnumbers @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); button login = (button)rootview.findviewbyid(r.id.w_login); wnumbers =(edittext)rootview.findviewbyid(r.id.w_number_edit_text); login.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { // todo auto-generated method stub string wnumber = wnumbers.gettext().tostring(); toast.maketext(getactivity(), wnumber, toast.length_long).show(); } }); return rootview; }
Comments
Post a Comment