2011-06-26 14 views
7
 
public class MainActivity extends Activity 
{ 
    private EditText editText; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     editText = (EditText) findViewById(R.id.etext); 
     if(editText == null) 
     { 
      Log.v("editText", "booohooo"); 
     } 
     else 
     { 
      Log.v("editText", "Success"); 
     } 

     final Button button = (Button) findViewById(R.id.gobutton); 

     button.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) 
      { 
       if(editText != null) 
       { 
        Log.v("editText", "is not NULL"); 
       } 
       else 
       { 
        Log.v("editText", "is NULL :("); 
       } 

       // Perform action on click 
       if(editText != null) 
       { 
        editText.getText(); 
       } 
       else 
       { 
        Log.v("editText", "is NULL"); 
       } 
       Log.v("url", editText.getText().toString().trim()); 
       Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(editText.getText().toString().trim())); 
       startActivity(browserIntent); 
      } 
     }); 
    } 
} 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView android_id="@+id/websiteurlheading" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter web site URL" 
    /> 
<EditText android_id="@+id/etext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/websiteurlheading" 
    /> 
<Button android:id="@+id/gobutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter" 
    /> 
</LinearLayout> 

cualquier ayuda se agradece.findViewById devuelve null para EditarTexto

+0

¿Cuál es el nombre de este archivo XML? – Haphazard

Respuesta

10

Asegúrese de que setContentView(R.layout.main); esté configurado correctamente. Si ha creado uno nuevo (que incluye el código xml anterior), utilícelo para establecer la vista de contenido: setContentView(R.layout.your_xml_filename);

+0

Acabo de modificar main.xml, por lo tanto, el código anterior debería haber funcionado. También el botón de obtener funciona. – jayesh

+0

Encontrado el problema..typo android_id debería haber sido android: id – jayesh

+0

Tuve el mismo problema y esta fue la causa. Gracias – xzdead

0

Estaba enfrentando este problema hace unos segundos y había buscado algunas publicaciones de Stackoverflow. Encontré la solución yo mismo.

Solo asegúrese de no llamar al editText.getText().toString(); en el método onCreate. Porque solo devolverá los valores precargados. Por lo tanto, editext siempre devolverá nulo en este caso.

1

cambio thes líneas en sus Vistas

android_id="@+id/websiteurlheading" 

android_id="@+id/etext" 

y el cambio View ID gusta esta

android:id="@+id/websiteurlheading" 
android:id="@+id/etext" 

por ejemplo

<TextView android:id="@+id/websiteurlheading" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter web site URL" 
    /> 
<EditText android:id="@+id/etext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/websiteurlheading" 
    /> 
0

Esto sucede si el diseño utilizado para setContentView la función no es igual que su disposición de EditBox.

Cuestiones relacionadas