I am a newbie to programming. Lately I am trying to study android & java on my own so I have very basic doubts please help me to clear them.

I want to display the text typed in EditText after clicking the button(using onClick attribute in XML file). My program works only if I find the EditText inside "Press" Method but not if find the text field globally. Can anyone explain it

Here is my code

public class MainActivity extends Activity {    
        String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void Press  (View view) {

         EditText input =(EditText) findViewById(R.id.main_input);
        text= input.getText().toString();

          Toast.makeText(this, text,

          Toast.LENGTH_LONG).show();
        } 
    }

XML file

<EditText
        android:id="@+id/main_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/start"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="86dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maiinput"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="178dp"
        android:onClick="Press"
        android:text="Start" />
有帮助吗?

解决方案

Try this way

public class MainActivity extends Activity {    
String text;
EditText input;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input =(EditText) findViewById(R.id.main_input);
}

public void clickme(View view) {
  text= input.getText().toString();
  Toast.makeText(MainActivity.this, text,Toast.LENGTH_LONG).show();
 } 
}

and change your onClick="clickme" to your Button in your activity_main.xml

其他提示

private EditText text;
private Button ok;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text=(EditText)findViewById(R.id.editText1);
    ok=(Button)findViewById(R.id.button1);
    ok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, text.getText().toString(), Toast.LENGTH_LONG).show();
        }
    });

}

You have to write in this way hope this works for you

From comments:

i tried to find the edittext below mainactivity itself like this public class MainActivity extends Activity { EditText input = (EditText) findViewById(R.id.main_input); but it didn't show any errors then but only after running it showed the error.can u explain y that happened

You're calling findViewById() too early when class member variables are initialized. At that point the activity does not have a Window yet and it will NPE.

You should call findViewById():

  • in onCreate() or later when there's the Window and no NPE occurs, and
  • after setContentView() when there's a view hierarchy to actually find something and non-null can be returned.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top