我正在尝试创建一个屏幕始终处于横向模式的应用。
我使用setRequestedOrientation()函数来实现它。我用了一些按钮来创建一个计算器。
但是当我尝试检查它的一些功能时,通过在模拟器上运行它,我得到了错误Unfortunately, Your app has been stopped
。
基本上,它会在按钮的nullpointer exception
功能上抛出setOnClickListener
。我不知道为什么会这样。
logcat错误:
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)
这是我的java代码。
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("^");
}
}
});
}
我正在尝试使用layout-land
资源。
答案 0 :(得分:1)
在将ORIENTATION_LANDSCAPE
设置为onClickListener
之前添加对Button
的检查。
试试这个:
.................
........................
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("^");
}
}
});
}