我的任务是在使用垂直方向时制作“常规”计算器,在横向时制作“技术”计算器。所以,起初,我做了两个方向的接口,并在模拟器上测试它以确保它工作。
对于横向,我在“res”中创建了一个文件夹“layout-land”,并放置了同名的 xml 文件,用于垂直方向。我只有一项活动。
据我了解,我为垂直方向的按钮编写的代码应该适用于横向方向具有相同 ID 的按钮吗?
然后,我为“常规”计算器编写了一些代码,它工作正常,但是当我决定改变方向时,我的应用程序停止并且 LogCat 说:
对于横向,我在“res”中创建了一个文件夹“layout-land”,并放置了同名的 xml 文件,用于垂直方向。我只有一项活动。
据我了解,我为垂直方向的按钮编写的代码应该适用于横向方向具有相同 ID 的按钮吗?
Lofcat 接下来说:
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
我的活动代码是这样的:
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