In Eclipse Avd I Get A "unfortunately, Calculator Has Stopped." Api Level 16
Solution 1:
Your problem most likely lies with the global View variables. Modify
TextViewanswer= (TextView) findViewById(R.id.answer);
ButtonButtonEquals= (Button) findViewById(R.id.ButtonEquals);
so it is just
TextView answer;
Button ButtonEquals;
Then in onCreate()
, do this:
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answer = (TextView) findViewById(R.id.answer);
ButtonEquals = (Button) findViewById(R.id.ButtonEquals);
}
Your app is probably crashing because of the fact that you are trying to access the Views before the Activity has anything to show. Putting your code after setContentView allows the Views to be found and you avoid NullPointerException
s
Also:
Your methods need to be methodName (View view)
if they are referenced from the onClick:
attribute
Most your methods need to be changed, but I'll take the ButtonEquals
as an example.
Your xml says
android:onClick = "equals"
So the method in your code needs to be
publicvoidequals(View v)
Adding the View
argument needs to be done for every method that the onClick:
attribute uses.
Solution 2:
1/ You can see exactly what's wrong by reading the logcat. It can be seen from Eclipse from the Java perspective or by using the command line:
adb logcat
2/ Move the instantiation of the *TextView*s to the onCreate() method after the setContentView() call.
Post a Comment for "In Eclipse Avd I Get A "unfortunately, Calculator Has Stopped." Api Level 16"