Reputation: 339
package com.parseador.prueba;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class main extends Activity {
/** Called when the activity is first created. */
//private Button botonParsear;
private EditText link;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Button botonParsear = (Button) findViewById(R.id.BotonParsear);
//EditText link = (EditText) findViewById(R.id.Link);
}
public void onParsearClick(View botonParsear) {
link.setText("Siii");
}
}
Hello.
Ive a problem when i run this code. It keeps FCing. Have tried changing whats on comments, but theres no way to make the button work.
I would appreciate if you could tell me what am i doing wrong.
Thanks in advance.
Ps: The xml file does have the method on the button. This is its code (on strong the call):
android:layout_height="wrap_content"
android:text="@string/parse"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:onClick="onParsearClick"
android:id="@+id/BotonParsear"
Upvotes: 1
Views: 250
Reputation: 28932
The line
EditText link = (EditText) findViewById(R.id.Link);
is almost there. You're declaring a new local variable link
that shadows the instance variable link
in your main
class. So while you assign the local variable to the View
instance located by findViewById
, it falls out of scope and ceases to exist after onCreate
returns. The link
variable read by onParsearClick
is still null
and cannot be dereferenced. Therefore the call to link.setText
fails.
Change the line so that it is a simple assignment statement rather than a variable declaration:
link = (EditText) findViewById(R.id.Link);
You may want to read up on Java's scoping rules for the future.
Upvotes: 1
Reputation: 11257
Your example(modified) works for me...
public void onParsearClick(View botonParsear) {
Toast.makeText(this, "it is working", Toast.LENGTH_LONG).show();
}
See if any of this thread applies to you - onClick won't fire on ImageView
Upvotes: 0