Question

ok so i am traing to make a profile app and it works making a series of questions and asigning a value to them at the end every answer its added and gives a text result for this i use the putExtra in order to keep track of the result, but when i try to go to the result activity it simply crashes, all activites are declared in the manifest

here is the first activity up to this point everything runs smoothly:

package com.springair.perfiladorsam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Donde extends Activity implements OnClickListener {
Button cama;
Button litera;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.donde);
    setupVariables();


}

private void setupVariables() {
    // TODO Auto-generated method stub
    Button cama = (Button) findViewById(R.id.cama);
    Button litera = (Button) findViewById(R.id.litera);
  cama.setOnClickListener(this);
  litera.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    Intent resultado = new Intent ("com.springair.perfilador.RESULTADO");
    Bundle extras = getIntent().getExtras();
    int resulta = extras.getInt("StoredData");

    switch(v.getId()){
    case R.id.Delg:
        resulta = resulta + 5;
        resultado.putExtra("StoredData", (int)resulta);
        startActivity(resultado);

        break;
    case R.id.Medio:
        resulta = resulta + 3;
        resultado.putExtra("StoredData", (int)resulta);
        startActivity(resultado);
        break;

    }
}
}

then its the result Activity: but it never reaches it:

package com.springair.perfiladorsam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Resultado extends Activity implements OnClickListener{

    Button reinicio;
    TextView Firmeza;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.resultado);
        super.onCreate(savedInstanceState);
        setupVariables();
        Bundle extras = getIntent().getExtras();
        int resulta = extras.getInt("StoredData");

        if(resulta<5){
            Firmeza.setText("Firme");
        }
        else if (resulta <10){
            Firmeza.setText("Medio");
        }
        else{
            Firmeza.setText("Suave");
        }
    }

    private void setupVariables() {
        // TODO Auto-generated method stub
        reinicio = (Button)findViewById(R.id.Reinicio);
        Firmeza = (TextView)findViewById(R.id.Nivel);
        reinicio.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent cheese = new Intent("com.springair.perfilador.MAINMENU");
        startActivity(cheese);
    }


}

¿What am I doing wrong? Again everything is declared in the manifest and the names are correct i have checked twice.

Here's the LogCat

01-31 23:16:46.684: E/AndroidRuntime(831): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.ActivityThread.access$1300(ActivityThread.java:141)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.os.Looper.loop(Looper.java:137)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.ActivityThread.main(ActivityThread.java:5041)
01-31 23:16:46.684: E/AndroidRuntime(831):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 23:16:46.684: E/AndroidRuntime(831):  at java.lang.reflect.Method.invoke(Method.java:511)
01-31 23:16:46.684: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 23:16:46.684: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 23:16:46.684: E/AndroidRuntime(831):  at dalvik.system.NativeStart.main(Native Method)
01-31 23:16:46.684: E/AndroidRuntime(831): Caused by: java.lang.NullPointerException
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
01-31 23:16:46.684: E/AndroidRuntime(831):  at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
Was it helpful?

Solution

The error is in your results activity. In the onCreate method you have

...
// TODO Auto-generated method stub
setContentView(R.layout.resultado);
super.onCreate(savedInstanceState);
setupVariables(); 
...

You need to move your call to super.onCreate(savedInstanceState); to the beginning of the method. When dealing with the overridden methods in the android lifecycle, some of the methods will have the super method called first, while others are called last. None of them though work with being called in between. Check the developer docs to be certain on when to call the supers in the future.

OTHER TIPS

In Resultado, you need to call super.onCreate(savedInstanceState) first, then call setContentView().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top