Home > Mobile >  New people for help, ask bosses to help solve it
New people for help, ask bosses to help solve it

Time:09-21

With android studio generated app always flash back when feeling also does not have an error in the eventlog is Mainactivity. The structural problems in the Java
Java code below
 package com. Example. Administrator. Calculate; 
The import android. App. The Activity;
The import android. OS. Bundle;
The import android. View. The view;
import android.widget.Button;
The import android. Widget. The EditText;

Public class MainActivity extends the Activity implements the View. An OnClickListener {

The Button btn_0;
The Button btn_1;
The Button btn_2;
The Button btn_3;
The Button btn_4;
The Button btn_5;
The Button btn_6;
The Button btn_7;
The Button btn_8;
The Button btn_9;
The Button btn_point;
The Button btn_clear;
The Button btn_del;
The Button btn_pluse;
The Button btn_minus;
The Button btn_multiply;
The Button btn_divide;
The Button btn_equle;
The EditText et_input;
Boolean clear_flag;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btn_1=(Button) the findViewById (R.i db utton13);
Btn_2=(Button) the findViewById (R.i db utton14);
Btn_3=(Button) the findViewById (R.i db utton15);
Btn_4=(Button) the findViewById (R.i db utton9);
Btn_5=(Button) the findViewById (R.i db utton10);
Btn_6=(Button) the findViewById (R.i db utton11);
Btn_7=(Button) the findViewById (R.i db utton5);
Btn_8=(Button) the findViewById (R.i db utton6);
Btn_9=(Button) the findViewById (R.i db utton7);
Btn_point=(Button) the findViewById (R.i db utton19);
Btn_clear=(Button) the findViewById (R.i db utton1);
Btn_del=(Button) the findViewById (R.i db utton4);
Btn_pluse=(Button) the findViewById (R.i db utton12);
Btn_minus=(Button) the findViewById (R.i db utton8);
Btn_multiply=(Button) the findViewById (R.i db utton3);
Btn_divide=(Button) the findViewById (R.i db utton2);
Btn_equle=(Button) the findViewById (R.i db utton20);
Et_input=(EditText) the findViewById (R.i d.e t_input);

Btn_0. SetOnClickListener (this);
Btn_1. SetOnClickListener (this);
Btn_2. SetOnClickListener (this);
Btn_3. SetOnClickListener (this);
Btn_4. SetOnClickListener (this);
Btn_5. SetOnClickListener (this);
Btn_6. SetOnClickListener (this);
Btn_7. SetOnClickListener (this);
Btn_8. SetOnClickListener (this);
Btn_9. SetOnClickListener (this);
Btn_point. SetOnClickListener (this);
Btn_clear. SetOnClickListener (this);
Btn_del. SetOnClickListener (this);
Btn_pluse. SetOnClickListener (this);
Btn_minus. SetOnClickListener (this);
Btn_multiply. SetOnClickListener (this);
Btn_divide. SetOnClickListener (this);
Btn_equle. SetOnClickListener (this);

}

@ Override
Public void onClick (View v) {
String STR=et_input. GetText (). The toString ();
The switch (v.g etId ()) {
Case R.i db utton18:
Case R.i db utton13:
Case R.i db utton14:
Case R.i db utton15:
Case R.i db utton9:
Case R.i db utton10:
Case R.i db utton11:
Case R.i db utton5:
Case R.i db utton6:
Case R.i db utton7:
Case R.i db utton19:
If (clear_flag) {
Clear_flag=false;
STR="";
Et_input. SetText (" ");
}
Et_input. SetText (STR + ((Button) v). The getText ());
break;
Case R.i db utton12:
Case R.i db utton8:
Case R.i db utton3:
Case R.i db utton2:
If (clear_flag) {
Clear_flag=false;
STR="";
Et_input. SetText (" ");
}
If (STR. The contains (" + ") | | STR. The contains (" - ") | | STR. The contains (" * ") | | STR. The contains ("/")) {
STR=STR. The substring (0, STR. IndexOf (" "));
}
Et_input. SetText (STR + "" + ((Button) v). The getText () +" ");
break;
Case R.i db utton1:
If (clear_flag)
Clear_flag=false;
STR="";
Et_input. SetText (" ");
break;
Case R.i db utton4://determine whether is empty, and then to delete the
If (clear_flag) {
Clear_flag=false;
STR="";
Et_input. SetText (" ");
} else if (STR!=null & amp; & ! STR. Equals (" ")) {
Et_input. SetText (STR. The substring (0, STR., length () - 1));
}
break;
Case R.i db utton20://separate operation and finally the results
GetResult ();//call the following method
break;
}
}

Private void getResult () {
String exp=et_input. GetText (). The toString ();
If (exp==null | | exp. Equals (" ")) return;
//because there is no operator need not operation so
if (! Exp. The contains (" ")) {
return;
}
If (clear_flag) {
Clear_flag=false;
return;
}
Clear_flag=true;
//interception of string in front of the operator
String s1=exp. The substring (0, exp. IndexOf (" "));
//interception operator
String op=exp. The substring (exp. IndexOf (" ") + 1, exp. IndexOf (" ") + 2);
//interception operator at the back of the string
String s2=exp. The substring (exp. IndexOf (" ") + 3);
Double CNT=0;
if (! S1. The equals (" ") & amp; & ! S2. Equals (" ")) {
Double d1=double. ParseDouble (s1);
Double d2=double. ParseDouble (s2);
If (op) equals (" + ")) {
CNT=d1 + d2;
}
If (op) equals (" - ")) {
CNT=d1, d2;
}
If (op) equals (" * ")) {
CNT=* d1 d2;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related