1

I am trying to make an app whose screen is always in landscape mode.

I used the setRequestedOrientation() function to implement that. I have used some buttons to create a calculator.

But when I tried to check some of its functionalities , by running it on emulator, I got the error Unfortunately, Your app has been stopped.

Basically, it is throwing nullpointer exception at the setOnClickListener functionality of the buttons. I have no clue why it is so. The logcat error:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.john.mycalci/com.example.john.mycalci.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.john.mycalci.MainActivity.onCreate(MainActivity.java:79)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

This is my java code.

TextView tvDisplay =(TextView)findViewById(R.id.tvDisplay);
    Button bBack =(Button) findViewById(R.id.bBack);
    final Button bRad =(Button) findViewById(R.id.bRad);
    final Button bDegree =(Button) findViewById(R.id.bDegree);
    Button bFact =(Button) findViewById(R.id.bFact);
    Button bRp =(Button) findViewById(R.id.bRp);
    Button bLp =(Button) findViewById(R.id.bLp);
    Button bPercentage =(Button) findViewById(R.id.bPercentage);
    Button bPower =(Button) findViewById(R.id.bPower);
    Button bInv =(Button) findViewById(R.id.bInv);
    final Button bSin =(Button) findViewById(R.id.bSin);
    final Button bLn =(Button) findViewById(R.id.bLn);
    Button bDivide =(Button) findViewById(R.id.bDivide);
    Button bPi =(Button) findViewById(R.id.bPi);
    final Button bCos=(Button) findViewById(R.id.bCos);
    final Button bLog=(Button) findViewById(R.id.bLog);
    Button bMultiply=(Button) findViewById(R.id.bMultiply);
    Button bExp=(Button) findViewById(R.id.bExp);
    final Button bTan=(Button) findViewById(R.id.bTan);
    final Button bSqrt=(Button) findViewById(R.id.bSqrt);
    Button bSubtract=(Button) findViewById(R.id.bSubtract);
    Button bEqual=(Button) findViewById(R.id.bEqual);
    Button bPower10=(Button) findViewById(R.id.bPowe10);
    final Button bExpo=(Button) findViewById(R.id.bExpo);
    Button bAns=(Button) findViewById(R.id.bAns);
    Button bAdd=(Button) findViewById(R.id.Add);
    Button bDot=(Button) findViewById(R.id.bDot);
    Button bOne=(Button) findViewById(R.id.bOne);
    Button bTwo=(Button) findViewById(R.id.bTwo);
    Button bThree=(Button) findViewById(R.id.bThree);
    Button bFour=(Button) findViewById(R.id.bFour);
    Button bFive=(Button) findViewById(R.id.bFive);
    Button bSix=(Button) findViewById(R.id.bSix);
    Button bSeven=(Button) findViewById(R.id.bSeven);
    Button bEight=(Button) findViewById(R.id.bEight);
    Button bNine=(Button) findViewById(R.id.bNine);


    bRad.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s = ((Button) v).getText().toString();
            if(s=="Rad") {
                bDegree.setText("Degree");
                bRad.setText("");
            }

        }
    });
    bDegree.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s = ((Button) v).getText().toString();
            if(s=="Rad") {
                bDegree.setText("");
                bRad.setText("Rad");
            }

        }
    });


    bInv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clicks += 1;
            if (clicks % 2 == 1) {
                bCos.setText(Html.fromHtml("cos<sup>-1</sup>"));
                bSin.setText(Html.fromHtml("sin<sup>-1</sup>"));
                bTan.setText(Html.fromHtml("tan<sup>-1</sup>"));
                bLn.setText(Html.fromHtml("e<sup>x</sup>"));
                bLog.setText(Html.fromHtml("10<sup>x</sup>"));
                bSqrt.setText(Html.fromHtml("x<sup>2</sup>"));
                bExpo.setText("root");
            }
            else{
                bCos.setText("cos");
                bSin.setText("sin");
                bTan.setText("tan");
                bLn.setText("ln");
                bLog.setText("log");
                bSqrt.setText("sqrt");
                bExpo.setText("^");

            }
        }
    });

}

I am trying to use the layout-land resource.

alayor
  • 4,537
  • 6
  • 27
  • 47
Apurva
  • 13
  • 3

1 Answers1

1

Add checking for ORIENTATION_LANDSCAPE before setting onClickListener to Button.

Try this:

.................
........................

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
{
    bRad.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s = ((Button) v).getText().toString();
            if(s=="Rad") {
                bDegree.setText("Degree");
                bRad.setText("");
            }

        }
    });
    bDegree.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s = ((Button) v).getText().toString();
            if(s=="Rad") {
                bDegree.setText("");
                bRad.setText("Rad");
            }

        }
    });


    bInv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clicks += 1;
            if (clicks % 2 == 1) {
                bCos.setText(Html.fromHtml("cos<sup>-1</sup>"));
                bSin.setText(Html.fromHtml("sin<sup>-1</sup>"));
                bTan.setText(Html.fromHtml("tan<sup>-1</sup>"));
                bLn.setText(Html.fromHtml("e<sup>x</sup>"));
                bLog.setText(Html.fromHtml("10<sup>x</sup>"));
                bSqrt.setText(Html.fromHtml("x<sup>2</sup>"));
                bExpo.setText("root");
            }
            else{
                bCos.setText("cos");
                bSin.setText("sin");
                bTan.setText("tan");
                bLn.setText("ln");
                bLog.setText("log");
                bSqrt.setText("sqrt");
                bExpo.setText("^");

            }
        }
    });

}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61