2010-07-14 14 views
5

Tengo una red de flotadores camObjCoord declarada como ..excepción de puntero nulo cuando se utiliza Bundle pasar datos

public static float camObjCoord[] = new float[8000];

entonces estoy llenando es índices en una clase que hace algo como lo siguiente. .

camObjCoord[1] = 2.5;

estoy llamando a continuación makeview()

 public void makeview() { 
    Intent myIntent = new Intent(this, GLCamTest.class); 
    this.startActivity(myIntent); 
    Bundle b = new Bundle(); 
    b.putFloatArray("tweets", camObjCoord); 
} 

y luego en la nueva clase que está haciendo ...

   public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bundle b = this.getIntent().getExtras(); 
    float original[] = b.getFloatArray("tweets"); 
    camObjCoord = original; 
    counter++; 
} 

... Pero me estoy poniendo un puntero nulo a excepción float original[] = b.getFloatArray("tweets"); y no sé qué. Intenté agrupar antes de invocar el intento, etc., pero no tuve suerte en una solución. Alguien sabe por qué?

También he incluido algunos de los errores en caso de que alguno de ustedes esté interesado.

  07-14 11:14:35.592: ERROR/AndroidRuntime(7886): Caused by: java.lang.NullPointerException 
      07-14 11:14:35.592: ERROR/AndroidRuntime(7886):  at org.digital.com.GLCamTest.onCreate(GLCamTest.java:41) 
      07-14 11:14:35.592: ERROR/AndroidRuntime(7886):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
      07-14 11:14:35.592: ERROR/AndroidRuntime(7886):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
      07-14 11:14:35.592: ERROR/AndroidRuntime(7886):  ... 11 more 

¡Gracias!

+0

Y qué línea es la línea 41? –

+0

@Jon Skeet 'float original [] = b.getFloatArray (" tweets ");' – Skizit

+1

De acuerdo, eso sugiere una respuesta. Por favor, brinde toda la información relevante como esta en futuras preguntas. –

Respuesta

4

Bien, entonces eso sugiere que this.getIntent().getExtras() ha devuelto null. Tenga en cuenta que en makeview no ha hecho nada después de crear el paquete. ¿Necesita hacer:

myIntent.putExtras(b); 

al final tal vez? (No soy un desarrollador de Android, así que no sé la API, pero que suena probable ...)

EDIT: Como otros han señalado, se debe poner potencialmente la startActivity llamada después de configurar todo en la intención

0

Si obtiene una NullPointerException en la línea de

float original[] = b.getFloatArray("tweets"); 

entonces la única opción es que b es nulo. ¿Puede this.getIntent(). GetExtras() devolver nulo en algunos casos? deberías verificarlo

1
public void makeview() { 
Intent myIntent = new Intent(this, GLCamTest.class); 
this.startActivity(myIntent); 
Bundle b = new Bundle(); 
b.putFloatArray("tweets", camObjCoord); 

}

Creo error que ha cometido es que ha iniciado la actividad this.startActivity(myIntent); y después de que usted está pasando el valor paquete

Bundle b = new Bundle(); 
    b.putFloatArray("tweets", camObjCoord); 

. ---->>> so it will pass nothing. por lo que en el lado receptor

Bundle b = this.getIntent().getExtras(); 
    float original[] = b.getFloatArray("tweets"); 

---->>> this above code receive "null" value.

así que trata de agrupar primero y luego comenzar su actividad.

Prueba esto:

public void makeview() { 
    Intent myIntent = new Intent(this, GLCamTest.class); 

    Bundle b = new Bundle(); 
    b.putFloatArray("tweets", camObjCoord); 

this.startActivity(myIntent); 

} 
3

hay una falla en su lógica método makeview, es necesario añadir los extras a la intención antes de que sea iniciado. También es muy recomendable utilizar una constante (GLCamTest.TWEETS) para la clave.

public void makeview() { 
    Intent myIntent = new Intent(this, GLCamTest.class); 
    myIntent.putExtra(GLCamTest.TWEETS, camObjCoord);//assuming camObjCoord is float[] 
    this.startActivity(myIntent); 
} 

Y en el otro lado

Bundle b = this.getIntent().getExtras(); 
float original[]; 
if (b!=null) { 
    original = b.getFloatArray(GLCamTest.TWEETS); 
} 
if (original!=null) { 
    //do something with original 
} 
0

this.getIntent(). GetExtras() ha vuelto nulo. Asegúrese de que en makeview no haya hecho nada después de crear el paquete. Necesita:

myIntent.putExtras (b);

al final

0

Bueno, tuve un problema similar. en mi caso, la excepción de punto nulo ocurrió cuando revisé si mi bundle.getString() era igual a nulo.

aquí es como en mi caso lo solucioné:

Intent intent = getIntent();   
    if(intent.hasExtra("nomeUsuario")){ 
     bd = getIntent().getExtras(); 
     if(!bd.getString("nomeUsuario").equals(null)){ 
      nomeUsuario = bd.getString("nomeUsuario"); 
     } 
    } 
Cuestiones relacionadas