gpt4 book ai didi

java - 在 button.setonclick 监听器中抛出空指针异常

转载 作者:行者123 更新时间:2023-11-30 00:32:23 27 4
gpt4 key购买 nike

我正在尝试制作一个屏幕始终处于横向模式的应用程序。

我使用 setRequestedOrientation() 函数来实现它。我使用了一些按钮来创建一个计算器。

但是当我试图通过在模拟器上运行它来检查它的一些功能时,我得到了错误Unfortunately, Your app has been stopped

基本上,它是在按钮的 setOnClickListener 功能处抛出 nullpointer exception。我不知道为什么会这样。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 资源。

最佳答案

在将 onClickListener 设置为 Button 之前添加对 ORIENTATION_LANDSCAPE 的检查。

试试这个:

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

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("^");

}
}
});

}

关于java - 在 button.setonclick 监听器中抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44052781/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com