2011-05-09 14 views
5

tengo código:findViewById devuelve null

public class HelloWorld extends Activity { 

private Button buttonOk; 
private Button buttonCancel; 

private OnClickListener buttonOkListener = new OnClickListener() { 
    public void onClick(View v){ 
     EditText editText = (EditText)findViewById(R.id.input); 

     CharSequence textFromInput = (CharSequence) editText.getText(); 
     Context context = getApplicationContext(); 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context,textFromInput,duration); 
     toast.show(); 
    } 
}; 

private OnClickListener buttonCancelListener = new OnClickListener() { 
    public void onClick(View v){ 
     EditText editText = (EditText)findViewById(R.id.input); 

     CharSequence textFromInput = (CharSequence) editText.getText(); 
     Context context = getApplicationContext(); 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context,textFromInput,duration); 
     toast.show(); 
    } 
}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    // ToDo add your GUI initialization code here 

    setContentView(R.layout.main); 

    buttonOk = (Button)findViewById(R.id.buttonOk); 
    buttonCancel = (Button)findViewById(R.id.buttonCancel); 

    buttonOk.setOnClickListener(buttonOkListener); 
    buttonCancel.setOnClickListener(buttonCancelListener); 
} 
} 

y el archivo de diseño:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Wpisz swoje imię:"/> 
<EditText 
    android:id="@+id/input" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:drawable/editbox_background" 
    android:layout_below="@id/label"/> 
<Button 
    android:id="@+id/buttonOk" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/input" 
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="10dip" 
    android:text="OK" /> 
<Button 
    android:id="@+id/buttonCancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/buttonOk" 
    android:layout_alignTop="@id/buttonOk" 
    android:text="Anuluj" /> 
</RelativeLayout> 

línea con buttonCancel.setOnClickListener (buttonCancelListener); arroja una excepción porque buttonCancel es nulo. que estoy haciendo mal?

+0

Estoy enfrentando el mismo problema. Aunque todo en el código parece correcto, FindViewById devuelve nulo con uno de los botones. – castle1971

Respuesta

10

no hay problema con sus códigos, por derecho todo debería funcionar como de costumbre.

El error más común de encontrar nulo a través del método findViewById es cuando se olvidó de llamar a setContentView o lo llamó por el diseño incorrecto.

¡Sugiero que limpie su proyecto y vuelva a intentarlo!

1

No estoy 100% seguro pero está llamando a findviewbyid en la inicialización de clase. Creo que este código se llama antes del método onCreate, por lo que no se puede encontrar la vista. La inicialización de los oyentes en el método oncreate debería funcionar.

2

Estaba teniendo el mismo problema, pero después de limpiar mi proyecto y ejecutarlo de nuevo funciona a la perfección.

Cuestiones relacionadas