我正在尝试构建一个简单的计算器,但它崩溃了。 我试图调试,我发现问题是数组。
这是我的代码:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText Scr;
private float NumberBf;
private String Operation;
private ButtonClickListener btnClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Scr = (EditText) findViewById(R.id.resultsBox);
int idList[] = {R.id.b0,R.id.b1,R.id.b2,R.id.b3,R.id.b4,R.id.b5,
R.id.b6,R.id.b7,R.id.b8,R.id.b9,R.id.bpoint,
R.id.clean,R.id.equals,R.id.half,R.id.minus,
R.id.multiply,R.id.plus};
for(int id:idList)
{
View v = (View) findViewById(id);
v.setOnClickListener(btnClick);
}
Log.i("for", "for ok");
setContentView(R.layout.activity_main);
}
public void getKeyboard(String str){
String ScrCurrect = Scr.getText().toString();
ScrCurrect += str;
Scr.setText(ScrCurrect);
}
public void mResult(){
float NumAf = Float.parseFloat(Scr.getText().toString());
float result = 0;
if(Operation.equals("+"))
{
result = NumAf + NumberBf;
}
if(Operation.equals("-"))
{
result = NumAf - NumberBf;
}
if(Operation.equals("*"))
{
result = NumAf * NumberBf;
}
if(Operation.equals("/"))
{
result = NumAf / NumberBf;
}
Scr.setText(String.valueOf(result));
}
public void mMath(String str){
NumberBf = Float.parseFloat(Scr.getText().toString());
Operation = str;
Scr.setText("0");
}
private class ButtonClickListener implements OnClickListener
{
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.clean:
Scr.setText("0");
NumberBf = 0;
Operation = "";
break;
case R.id.plus:
mMath("+");
break;
case R.id.minus:
mMath("-");
break;
case R.id.multiply:
mMath("*");
break;
case R.id.half:
mMath("/");
break;
case R.id.equals:
mResult();
break;
default:
String numb = ((Button)v).getText().toString();
getKeyboard(numb);
break;
}
}
}
}
我的Logcat是:
> 12-07 21:46:18.242: I/log(20993): id list ok 12-07 21:46:18.242:
> D/AndroidRuntime(20993): Shutting down VM 12-07 21:46:18.242:
> W/dalvikvm(20993): threadid=1: thread exiting with uncaught exception
> (group=0x417cac08) 12-07 21:46:18.242: E/AndroidRuntime(20993): FATAL
> EXCEPTION: main 12-07 21:46:18.242: E/AndroidRuntime(20993): Process:
> com.test.calculator, PID: 20993 12-07 21:46:18.242:
> E/AndroidRuntime(20993): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.test.calculator/com.test.calculator.MainActivity}:
> java.lang.NullPointerException 12-07 21:46:18.242:
> E/AndroidRuntime(20993): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
> 12-07 21:46:18.242: E/AndroidRuntime(20993): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
> 12-07 21:46:18.242: E/AndroidRuntime(20993): at
> android.app.ActivityThread.access$800(ActivityThread.java:138) 12-07
> 21:46:18.242: E/AndroidRuntime(20993): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
> 12-07 21:46:18.242: E/AndroidRuntime(20993): at
> android.os.Handler.dispatchMessage(Handler.java:102) 12-07
> 21:46:18.242: E/AndroidRuntime(20993): at
> android.os.Looper.loop(Looper.java:136) 12-07 21:46:18.242:
> E/AndroidRuntime(20993): at
> android.app.ActivityThread.main(ActivityThread.java:5034) 12-07
> 21:46:18.242: E/AndroidRuntime(20993): at
> java.lang.reflect.Method.invokeNative(Native Method) 12-07
> 21:46:18.242: E/AndroidRuntime(20993): at
> java.lang.reflect.Method.invoke(Method.java:515) 12-07 21:46:18.242:
> E/AndroidRuntime(20993): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
> 12-07 21:46:18.242: E/AndroidRuntime(20993): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611) 12-07
> 21:46:18.242: E/AndroidRuntime(20993): at
> dalvik.system.NativeStart.main(Native Method) 12-07 21:46:18.242:
> E/AndroidRuntime(20993): Caused by: java.lang.NullPointerException
> 12-07 21:46:18.242: E/AndroidRuntime(20993): at
> com.test.calculator.MainActivity.onCreate(MainActivity.java:34) 12-07
> 21:46:18.242: E/AndroidRuntime(20993): at
> android.app.Activity.performCreate(Activity.java:5241) 12-07
> 21:46:18.242: E/AndroidRuntime(20993): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 12-07 21:46:18.242: E/AndroidRuntime(20993): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
> 12-07 21:46:18.242: E/AndroidRuntime(20993): ... 11 more 12-07
> 21:46:18.265: I/Process(20993): Sending signal. PID: 20993 SIG: 9
> 12-07 21:46:52.890: D/OpenGLRenderer(21120): Enabling debug mode 0
> 12-07 21:46:53.148: I/Timeline(21120): Timeline: Activity_idle id:
> android.os.BinderProxy@41db95b8 time:61102416 12-07 21:47:22.859:
> I/Timeline(21120): Timeline: Activity_idle id:
> android.os.BinderProxy@41db95b8 time:61132128 12-07 21:51:53.742:
> I/Timeline(21271): Timeline: Activity_idle id:
> android.os.BinderProxy@41db1b50 time:61403009 12-07 21:51:59.390:
> D/AndroidRuntime(21544): Shutting down VM 12-07 21:51:59.390:
> W/dalvikvm(21544): threadid=1: thread exiting with uncaught exception
> (group=0x417cac08) 12-07 21:51:59.390: E/AndroidRuntime(21544): FATAL
> EXCEPTION: main 12-07 21:51:59.390: E/AndroidRuntime(21544): Process:
> com.test.calculator, PID: 21544 12-07 21:51:59.390:
> E/AndroidRuntime(21544): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.test.calculator/com.test.calculator.MainActivity}:
> java.lang.NullPointerException 12-07 21:51:59.390:
> E/AndroidRuntime(21544): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
> 12-07 21:51:59.390: E/AndroidRuntime(21544): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
> 12-07 21:51:59.390: E/AndroidRuntime(21544): at
> android.app.ActivityThread.access$800(ActivityThread.java:138) 12-07
> 21:51:59.390: E/AndroidRuntime(21544): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
> 12-07 21:51:59.390: E/AndroidRuntime(21544): at
> android.os.Handler.dispatchMessage(Handler.java:102) 12-07
> 21:51:59.390: E/AndroidRuntime(21544): at
> android.os.Looper.loop(Looper.java:136) 12-07 21:51:59.390:
> E/AndroidRuntime(21544): at
> android.app.ActivityThread.main(ActivityThread.java:5034) 12-07
> 21:51:59.390: E/AndroidRuntime(21544): at
> java.lang.reflect.Method.invokeNative(Native Method) 12-07
> 21:51:59.390: E/AndroidRuntime(21544): at
> java.lang.reflect.Method.invoke(Method.java:515) 12-07 21:51:59.390:
> E/AndroidRuntime(21544): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
> 12-07 21:51:59.390: E/AndroidRuntime(21544): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611) 12-07
> 21:51:59.390: E/AndroidRuntime(21544): at
> dalvik.system.NativeStart.main(Native Method) 12-07 21:51:59.390:
> E/AndroidRuntime(21544): Caused by: java.lang.NullPointerException
> 12-07 21:51:59.390: E/AndroidRuntime(21544): at
> com.test.calculator.MainActivity.onCreate(MainActivity.java:34) 12-07
> 21:51:59.390: E/AndroidRuntime(21544): at
> android.app.Activity.performCreate(Activity.java:5241) 12-07
> 21:51:59.390: E/AndroidRuntime(21544): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 12-07 21:51:59.390: E/AndroidRuntime(21544): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
> 12-07 21:51:59.390: E/AndroidRuntime(21544): ... 11 more 12-07
> 21:52:02.109: I/Process(21544): Sending signal. PID: 21544 SIG: 9