Principiante Android Development: findViewById issue
Consigo el mensaje de error siguiente al funcionar mi uso básico. La aplicación consiste en un botón llamado button1 y un textView llamado topLeft.
07-01 15:33:02.754: D/AndroidRuntime(2334): Shutting down VM 07-01 15:33:02.754: W/dalvikvm(2334): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 07-01 15:33:02.984: E/AndroidRuntime(2334): FATAL EXCEPTION: main 07-01 15:33:02.984: E/AndroidRuntime(2334): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.ActivityThread.access$600(ActivityThread.java:130) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.os.Handler.dispatchMessage(Handler.java:99) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.os.Looper.loop(Looper.java:137) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.ActivityThread.main(ActivityThread.java:4745) 07-01 15:33:02.984: E/AndroidRuntime(2334): at java.lang.reflect.Method.invokeNative(Native Method) 07-01 15:33:02.984: E/AndroidRuntime(2334): at java.lang.reflect.Method.invoke(Method.java:511) 07-01 15:33:02.984: E/AndroidRuntime(2334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 07-01 15:33:02.984: E/AndroidRuntime(2334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-01 15:33:02.984: E/AndroidRuntime(2334): at dalvik.system.NativeStart.main(Native Method) 07-01 15:33:02.984: E/AndroidRuntime(2334): Caused by: java.lang.NullPointerException 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.Activity.findViewById(Activity.java:1825) 07-01 15:33:02.984: E/AndroidRuntime(2334): at com.example.myfirstapp.MainActivity.<init>(MainActivity.java:28) 07-01 15:33:02.984: E/AndroidRuntime(2334): at java.lang.Class.newInstanceImpl(Native Method) 07-01 15:33:02.984: E/AndroidRuntime(2334): at java.lang.Class.newInstance(Class.java:1319) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 07-01 15:33:02.984: E/AndroidRuntime(2334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974) 07-01 15:33:02.984: E/AndroidRuntime(2334): ... 11 more
Al intentar utilizar el findViewById , este error aparece refiriéndose a la línea 28.
- RelativeLayout y el porcentaje de altura. No se puede usar layout_below y layout_weight
- RecyclerView con la imagen de fondo en la parte superior
- Uso de vistas personalizadas en XML sin utilizar el nombre de clase totalmente calificado
- Añadiendo un lineal lineal dinámico a la vista actual android
- Creación de una barra de herramientas personalizada en android
El programa se parece a lo siguiente:
package com.example.myfirstapp; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ int a = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } Button button1 = (Button) findViewById(R.id.button1); TextView topLeft = (TextView) findViewById(R.id.textView2); // button1.setOnClickListener( this ); public void onClick(View v) { // TODO Auto-generated method stub } }
¿Qué significa esto? ¿Por qué mi código tiene problemas para hacer algo a lo largo de las líneas de referencia al archivo R.java?
R.java does have public static final class id { public static final int button1=0x7f080001; public static final int menu_settings=0x7f080003; public static final int textView1=0x7f080000; public static final int textView2=0x7f080002;
Gracias por adelantado.
- ¿Cuántas tiendas de aplicaciones de Android para enviar ...?
- ¿Hacer Android TextView invisible hasta que presione el botón?
- Crea tu propia barra de acción en android
- Nueva interfaz de la tarjeta Google Now y Google+
- El elemento ListView no extenderá el ancho a fill_parent
- GetMeasuredHeight () y el ancho que devuelve 0 después de measure ()
- FragmentTabHost - No se conoce ninguna pestaña null
- Cómo establecer un texto sobre una imagen?
Usted está llamando a findViewById
como parte de su definición de campo, momento en el que el diseño no se infla (que sucede cuando llama a setContentView
). findViewById
no está encontrando la vista, por lo que la referencia se establece en null
.
Mover sus llamadas findViewById
dentro de onCreate
, después de llamar a setContentView
debería arreglarlo.
- ¿Hay algo como NSDictionary en android?
- Filtro personalizado de ListView da el elemento incorrecto seleccionado cuando se filtra