Lanitka
Lanitka

Reputation: 932

RuntimeException: Unable to start activity

My task is to make "regular" calculator when using vertical orientation and "technical" one for landscape orientation. So, at first, I did the interface for both orientations, and test it on emulator to make sure it works.

For landscape orientation I created a folder "layout-land" in "res", and put xml file with the same name, that I use for vertical orientation. I have only one activity.

As I understand the code that I wrote for buttons in vertical orientation should work for buttons with the same Id in landscape orientation?

Then, I wrote some code for "regular" calculator, it works fine, but when I decided to change orientation my application stops and LogCat says smth like:

For landscape orientation I created a folder "layout-land" in "res", and put xml file with the same name, that I use for vertical orientation. I have only one activity.

As I understand the code that I wrote for buttons in vertical orientation should work for buttons with the same Id in landscape orientation?

Lofcat says next:

09-20 10:10:37.544: D/AndroidRuntime(450): Shutting down VM
09-20 10:10:37.555: W/dalvikvm(450): threadid=1: thread exiting with uncaught exception (group=0x40014760)
09-20 10:10:37.564: E/AndroidRuntime(450): FATAL EXCEPTION: main
09-20 10:10:37.564: E/AndroidRuntime(450): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculatorapp/com.example.calculatorapp.MainActivity}: java.lang.NullPointerException
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.os.Looper.loop(Looper.java:132)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.ActivityThread.main(ActivityThread.java:4123)
09-20 10:10:37.564: E/AndroidRuntime(450):  at java.lang.reflect.Method.invokeNative(Native Method)
09-20 10:10:37.564: E/AndroidRuntime(450):  at java.lang.reflect.Method.invoke(Method.java:491)
09-20 10:10:37.564: E/AndroidRuntime(450):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-20 10:10:37.564: E/AndroidRuntime(450):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-20 10:10:37.564: E/AndroidRuntime(450):  at dalvik.system.NativeStart.main(Native Method)
09-20 10:10:37.564: E/AndroidRuntime(450): Caused by: java.lang.NullPointerException
09-20 10:10:37.564: E/AndroidRuntime(450):  at com.example.calculatorapp.MainActivity.onCreate(MainActivity.java:127)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.Activity.performCreate(Activity.java:4397)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09-20 10:10:37.564: E/AndroidRuntime(450):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
09-20 10:10:37.564: E/AndroidRuntime(450):  ... 11 more
09-20 10:10:42.074: I/Process(450): Sending signal. PID: 450 SIG: 9

My activity code is something like this:

public class MainActivity extends Activity {

    Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnDot, btnDel, btnCE, btnC, 
    btnPlus, btnMinus, btnMult, btnDiv, btnEqual, btn1divX, btnPercent, btnSqrt, btnSign, btnMC, btnMR, btnMS, btnMplus, btnMminus;   

    TextView currentView;

    StringBuilder numberStringBuilder;

    double memory;
    boolean memoryIsFull;


    char sign;
    double leftOperand, rightOperand;
    boolean signWasPressedOnce;
    boolean equalsignWasPressedOnce;

    boolean leftOperandIsReady;
    boolean rightOperandIsReady;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        numberStringBuilder = new StringBuilder();

        btn0 = (Button)findViewById(R.id.btn0);
        btn1 = (Button)findViewById(R.id.btn1);
......... //other buttons
                btnMS.setEnabled(false);

        currentView = (TextView)findViewById(R.id.display);

        leftOperand = rightOperand = 0;

        signWasPressedOnce = true;
        equalsignWasPressedOnce = true;
        leftOperandIsReady = false;
        rightOperandIsReady = false;
        memoryIsFull = false;

        OnClickListener onClickNumber = new OnClickListener() {

            @Override
            public void onClick(View v) {
                switch(v.getId()) {
                case R.id.btn0:
                    numberStringBuilder.append("0");
                    break;
..............//some other buttons
                btn0.setOnClickListener(onClickNumber);
        btn1.setOnClickListener(onClickNumber);
..............//some other setOnClick opeartions
                OnClickListener onClickOperation = new OnClickListener() {

            @Override
            public void onClick(View v) {
                double valueFromDisplay;
                String currentViewFromDisplay;
                switch(v.getId()) {
                case R.id.btnPlus:
                    if(signWasPressedOnce) 
                        actAsSignWasPressedOnce('+');
                    else 
                        actAsSignWasPressedMoreThanOnce('+');
                    break;
..............//some other buttons
                btnPlus.setOnClickListener(onClickOperation);
        btnMinus.setOnClickListener(onClickOperation);
................// and some methods

Upvotes: 1

Views: 1084

Answers (1)

User0911
User0911

Reputation: 1582

Possible reason could be, you are using an element like EditText or TextView that is only available in layout folder's xml file and not layout-land folder's xml file.

So, when you change the orientation, the layout file is not having that element and as onCreate is called again on each orientation change, it does not find that id and throws null pointer exception.

Please check both the layout xml files. I guess you are missing some element in any one of them, and using it's reference in MainActivity.java

Your logcat clearly says it, where is the error -

Caused by: java.lang.NullPointerException 09-20 10:10:37.564: at
android.app.Instrumentation.callActivityOnCreate

Hope that helps.

Upvotes: 2

Related Questions